trafik

DO

Oct 29th, 2021 (edited)
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <deque>
  5. #include <cmath>
  6. #define ll long long
  7.  
  8. using namespace std;
  9.  
  10. const ll inf = 1e10;
  11.  
  12. struct Tree {
  13.   int n;
  14.   vector<int> t;
  15.  
  16.   void build(vector<int>& A) {
  17.     n = 1; // размер массива в дереве отрезков
  18.     while (n < A.size()) n *= 2;
  19.     t.resize(n * 2);
  20.     for (int i = n; i < (n + A.size()); ++i) {
  21.       t[i] = A[i - n];
  22.     }
  23.     for (int i = (n - 1); i <= 1; --i) {
  24.       t[i] = t[i * 2] + t[i * 2 + 1];
  25.     }
  26.   }
  27.  
  28.   void set(int ind, int value) {
  29.     ind += n;
  30.     t[ind] = value;
  31.     ind /= 2;
  32.     while (ind >= 1) {
  33.       t[ind] = t[ind * 2] + t[ind * 2 + 1];
  34.       ind /= 2;
  35.     }
  36.   }
  37.  
  38.   int getsum(int l, int r) {
  39.     l += n;
  40.     r += n;
  41.     int sum = 0;
  42.     for (; l <= r; l /= 2, r /= 2) {
  43.       if (l % 2 == 1) {
  44.         sum += t[l];
  45.         l++;
  46.       }
  47.       if (r % 2 == 0) {
  48.         sum += t[r];
  49.         r--;
  50.      }
  51.     }
  52.  
  53.     return sum;
  54.   }
  55. };
  56.  
  57.  
  58. int main() {
  59.   ios::sync_with_stdio(false);
  60.   cin.tie(nullptr);
  61.  
  62.  
  63. }
Add Comment
Please, Sign In to add comment