jainbot27

lseg

Aug 26th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. using node = ll;
  2.  
  3. struct lazy{
  4.     int size;
  5.     vector<node> operations;
  6.     vector<node> vals;
  7.     const node NEUTRAL_ELEMENT = 0;
  8.     const node NO_OPERATION = 1e18;
  9.     const node START_VAL = 0;
  10.     ll query_op(node a, node b, int len){
  11.         if(b == NO_OPERATION) return a;
  12.         return b * len;
  13.     }
  14.     ll calc_op(node a, node b){
  15.         return a + b;
  16.     }
  17.     void apply_mod_op(node & a, node b, int c){
  18.         a = query_op(a, b, c);
  19.     }
  20.     void propogate(int x, int lx, int rx){
  21.         if(rx - lx == 1) return;
  22.         int m = (lx + rx)/2;
  23.         apply_mod_op(operations[2*x+1], operations[x], 1);
  24.         apply_mod_op(vals[2*x+1], operations[x], m-lx);
  25.         apply_mod_op(operations[2*x+2], operations[x], 1);
  26.         apply_mod_op(vals[2*x+2], operations[x], rx-m);
  27.         operations[x] = NO_OPERATION;
  28.     }
  29.     void build(int x, int lx, int rx){
  30.         if(rx == lx + 1){
  31.             vals[x] = START_VAL;
  32.             return;
  33.         }
  34.         int m = (lx + rx)/2;
  35.         build(2*x + 1, lx, m);
  36.         build(2*x + 2, m, rx);
  37.         vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
  38.     }
  39.     void init(int n){
  40.         size = 1;
  41.         while(size < n) size *= 2;
  42.         operations.assign(2 * size, 0);
  43.         vals.assign(2 * size, 0);
  44.         // build(0, 0, size);
  45.     }
  46.     void update(int l, int r, node v, int x, int lx, int rx){
  47.         propogate(x, lx, rx);
  48.         if(lx >= r || l >= rx) return;
  49.         if(lx >= l && rx <= r){
  50.             apply_mod_op(operations[x], v, 1);
  51.             apply_mod_op(vals[x], v, rx - lx);
  52.             return;
  53.         }
  54.         int m = (lx + rx)/2;
  55.         update(l, r, v, 2 * x + 1, lx , m);
  56.         update(l, r, v, 2 * x + 2, m , rx);
  57.         vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
  58.     }
  59.     void update(int l, int r, ll v){
  60.         update(l, r, v, 0, 0, size);
  61.     }
  62.     node query(int l, int r, int x, int lx, int rx){
  63.         propogate(x, lx, rx);
  64.         if(lx >= r || l >= rx) return NEUTRAL_ELEMENT;
  65.         if(lx >= l && rx <= r){
  66.             return vals[x];
  67.         }
  68.         int m = (lx + rx)/2;
  69.         node m1 = query(l, r, 2 * x + 1, lx, m);
  70.         node m2 = query(l, r, 2 * x + 2, m, rx);
  71.         return calc_op(m1, m2);
  72.     }
  73.     node query(int l , int r){
  74.         return query(l, r, 0, 0, size);
  75.     }
  76. } st;
  77.  
Advertisement
Add Comment
Please, Sign In to add comment