Advertisement
vlatkovski

broken segment tree lazy propagation

Mar 20th, 2019
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 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 mod = 1e9 + 9;
  10. const int maxn = 300005;
  11.  
  12. int n;
  13. int a[maxn];
  14. ll tree[3*maxn], lazy[3*maxn];
  15.  
  16. void updateRange(int k, int x, int y, int a, int b, int v) {
  17.     if (lazy[k] != 0) {
  18.         // lazy propagation
  19.         tree[k] += ll(y - x + 1) * lazy[k];
  20.         tree[k] %= mod;
  21.         if (x != y) {
  22.             lazy[2*k] += lazy[k];
  23.             lazy[2*k] %= mod;
  24.             lazy[2*k+1] += lazy[k];
  25.             lazy[2*k+1] %= mod;
  26.         }
  27.         lazy[k] = 0;
  28.     }
  29.     if (x > b || y < a) {
  30.         // completely out of range
  31.         return;
  32.     }
  33.     if (a <= x && y <= b) {
  34.         // completely in range
  35.         tree[k] += ll(y - x + 1) * v;
  36.         tree[k] %= mod;
  37.         if (x != y) {
  38.             lazy[2*k] += v;
  39.             lazy[2*k+1] += v;
  40.         }
  41.     } else {
  42.         // partially in range
  43.         int d = (x + y) / 2;
  44.         updateRange(2*k, x, d, a, b, v);
  45.         updateRange(2*k+1, d+1, y, a, b, v);
  46.         tree[k] = (tree[2*k] + tree[2*k+1]) % mod;
  47.     }
  48. }
  49.  
  50. ll sumRange(int k, int x, int y, int a, int b) {
  51.     if (x > b || y < a) {
  52.         // completely out of range
  53.         return 0;
  54.     }
  55.     if (lazy[k] != 0) {
  56.         // lazy propagation
  57.         tree[k] += ll(y - x + 1) * lazy[k];
  58.         if (x != y) {
  59.             lazy[2*k] += lazy[k];
  60.             lazy[2*k] %= mod;
  61.             lazy[2*k+1] += lazy[k];
  62.             lazy[2*k+1] %= mod;
  63.         }
  64.         lazy[k] = 0;
  65.     }
  66.     if (a <= x && y <= b) {
  67.         // completely in range
  68.         return tree[k];
  69.     } else {
  70.         int d = (x + y) / 2;
  71.         return (sumRange(2*k, x, d, a, b) + sumRange(2*k+1, d+1, y, a, b)) % mod;
  72.     }
  73. }
  74.  
  75. void Main() {
  76.     cin >> n;
  77.     for (int i = 0; i < n; ++i) {
  78.         cin >> a[i];
  79.     }
  80.     while (true) {
  81.         int t, l, r;
  82.         cin >> t >> l >> r;
  83.         --l, --r;
  84.         if (t == 1) {
  85.             int x;
  86.             cin >> x;
  87.             updateRange(1, 0, n-1, l, r, x);
  88.         } else {
  89.             int ans = sumRange(1, 0, n-1, l, r);
  90.             cout << ans << '\n';
  91.         }
  92.     }
  93. }
  94.  
  95. int main() {
  96. //    ios::sync_with_stdio(false);
  97. //    cin.tie(0);
  98.     #ifndef _DEBUG
  99.     #endif
  100.     Main();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement