jainbot27

Untitled

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