Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using node = ll;
- struct lazy{
- int size;
- vector<node> operations;
- vector<node> vals;
- const node NEUTRAL_ELEMENT = 0;
- const node NO_OPERATION = 1e18;
- const node START_VAL = 0;
- node query_op(node a, node b, int len){
- if(b == NO_OPERATION) return a;
- return b * len;
- }
- node calc_op(node a, node b){
- return a + b;
- }
- void apply_mod_op(node & a, node b, int c){
- a = query_op(a, b, c);
- }
- void propogate(int x, int lx, int rx){
- if(rx - lx == 1) return;
- int m = (lx + rx)/2;
- apply_mod_op(operations[2*x+1], operations[x], 1);
- apply_mod_op(vals[2*x+1], operations[x], m-lx);
- apply_mod_op(operations[2*x+2], operations[x], 1);
- apply_mod_op(vals[2*x+2], operations[x], rx-m);
- operations[x] = NO_OPERATION;
- }
- void build(int x, int lx, int rx){
- if(rx == lx + 1){
- vals[x] = START_VAL;
- return;
- }
- int m = (lx + rx)/2;
- build(2*x + 1, lx, m);
- build(2*x + 2, m, rx);
- vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
- }
- void init(int n){
- size = 1;
- while(size < n) size *= 2;
- operations.assign(2 * size, 0);
- vals.assign(2 * size, 0);
- // build(0, 0, size);
- }
- void update(int l, int r, node v, int x, int lx, int rx){
- propogate(x, lx, rx);
- if(lx >= r || l >= rx) return;
- if(lx >= l && rx <= r){
- apply_mod_op(operations[x], v, 1);
- apply_mod_op(vals[x], v, rx - lx);
- return;
- }
- int m = (lx + rx)/2;
- update(l, r, v, 2 * x + 1, lx , m);
- update(l, r, v, 2 * x + 2, m , rx);
- vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
- }
- void update(int l, int r, node v){
- update(l, r, v, 0, 0, size);
- }
- node query(int l, int r, int x, int lx, int rx){
- propogate(x, lx, rx);
- if(lx >= r || l >= rx) return NEUTRAL_ELEMENT;
- if(lx >= l && rx <= r){
- return vals[x];
- }
- int m = (lx + rx)/2;
- node m1 = query(l, r, 2 * x + 1, lx, m);
- node m2 = query(l, r, 2 * x + 2, m, rx);
- return calc_op(m1, m2);
- }
- node query(int l , int r){
- return query(l, r, 0, 0, size);
- }
- } st;
Advertisement
Add Comment
Please, Sign In to add comment