Advertisement
vlatkovski

Segment tree (range addition, range sum)

Apr 6th, 2019
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<int, int> pi;
  5. typedef vector<int> vi;
  6.  
  7.  
  8.  
  9. const int inf = 1e9;
  10. const int MAXN = 300005;
  11.  
  12. int n;
  13. int arr[MAXN];
  14.  
  15. int st[4*MAXN];
  16. int lazy[4*MAXN];
  17.  
  18. void build(int v, int tl, int tr) {
  19.     if (tl == tr) {
  20.         st[v] = arr[tl];
  21.     } else {
  22.         int tm = (tl + tr) / 2;
  23.         build(2*v, tl, tm);
  24.         build(2*v+1, tm+1, tr);
  25.         st[v] = st[2*v] + st[2*v+1];
  26.     }
  27. }
  28.  
  29. void push(int v, int tl, int tr) {
  30.     int tm = (tl + tr) / 2;
  31.     st[2*v] += (tm - tl + 1) * lazy[v];
  32.     lazy[2*v] += lazy[v];
  33.     st[2*v+1] += (tr - (tm + 1) + 1) * lazy[v];
  34.     lazy[2*v+1] += lazy[v];
  35.     lazy[v] = 0;
  36. }
  37.  
  38. void update(int v, int tl, int tr, int l, int r, int toadd) {
  39.     if (l > r) return;
  40.     if (l == tl && tr == r) {
  41.         st[v] += (tr - tl + 1) * toadd;
  42.         lazy[v] += toadd;
  43.     } else {
  44.         push(v, tl, tr);
  45.         int tm = (tl + tr) / 2;
  46.         update(2*v, tl, tm, l, min(r, tm), toadd);
  47.         update(2*v+1, tm+1, tr, max(l, tm+1), r, toadd);
  48.         st[v] = st[2*v] + st[2*v+1];
  49.     }
  50. }
  51.  
  52. int query(int v, int tl, int tr, int l, int r) {
  53.     if (l > r) return 0;
  54.     if (l <= tl && tr <= r) {
  55.         return st[v];
  56.     } else {
  57.         push(v, tl, tr);
  58.         int tm = (tl + tr) / 2;
  59.         return query(2*v, tl, tm, l, min(r, tm)) +
  60.                query(2*v+1, tm+1, tr, max(l, tm+1), r);
  61.     }
  62. }
  63.  
  64. void Main() {
  65.     cout << "Enter N, the size of the array, and then N numbers\n";
  66.     cin >> n;
  67.     for (int i = 0; i < n; ++i) {
  68.         cin >> arr[i];
  69.     }
  70.     build(1, 0, n-1);
  71.     cout << "First type of query:\n";
  72.     cout << "--> 1 l r\n";
  73.     cout << "--> Get the sum of elements in range [l, r]\n";
  74.     cout << "Second type of query:\n";
  75.     cout << "--> 2 l r v\n";
  76.     cout << "--> Add v to elements in range [l, r]\n";
  77.     while (true) {
  78.         int t;
  79.         cin >> t;
  80.         if (t == 1) {
  81.             int l, r;
  82.             cin >> l >> r;
  83.             cout << "sum[" << l << ", " << r << "] = " << query(1, 0, n-1, l, r) << "\n";
  84.         } else {
  85.             int l, r, x;
  86.             cin >> l >> r >> x;
  87.             update(1, 0, n-1, l, r, x);
  88.         }
  89.     }
  90. }
  91.  
  92. int main() {
  93. //    ios::sync_with_stdio(false);
  94. //    cin.tie(0);
  95.     #ifdef _DEBUG
  96. //    freopen("in.txt", "r", stdin);
  97. //    freopen("out.txt", "w", stdout);
  98.     #endif
  99.     #ifndef _DEBUG
  100.     #endif
  101.     Main();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement