Skip to content

Commit

Permalink
made lazy propagation template general and not problem specific
Browse files Browse the repository at this point in the history
  • Loading branch information
AarushJ committed Feb 9, 2019
1 parent f656b7e commit 83e8f1e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 11 deletions.
26 changes: 15 additions & 11 deletions Data structures/Lazy_propagation_range_sum_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ vector<ll> tree(4*N), lazy(4*N);

ll merge( ll left, ll right){
ll tmp;
tmp = left + right ;
// build temp from left and right according to quest
return tmp;
}

Expand All @@ -12,24 +12,28 @@ void ulazy( ll pos, ll lo, ll hi, ll qlo, ll qhi, ll val){
ll l = (pos<<1)+1, r = l+1;

if( lazy[pos] ){ // if there are pending updates to current node
tree[pos] += (hi-lo+1)*lazy[pos];

// Here, add lazy[pos] effect to tree[pos](Current node)

// lazy propagation to child nodes if they exist
if( lo != hi ){
lazy[l] += lazy[pos];
lazy[r] += lazy[pos];

}


// clear lazy[pos]
lazy[pos] = 0;
}

if( lo > hi || lo > qhi || hi < qlo )
return;

if( qlo <= lo && hi <= qhi ){ // queried segment totally overlaps the current range of function
tree[pos] += (hi-lo+1)*val;

// add effect of entire range to tree[pos]

// Add lazy to child nodes if they exist
if( lo != hi ){
lazy[l] += val;
lazy[r] += val;

}
return;
}
Expand All @@ -51,11 +55,11 @@ ll qlazy( ll pos, ll lo, ll hi, ll qlo, ll qhi){
return 0;

if( lazy[pos] ){ // if there are pending updates to current node
tree[pos] += (hi-lo+1)*lazy[pos];
// Here, add lazy[pos] effect to tree[pos](Current node)

// lazy propagation to child nodes if they exist
if( lo != hi ){
lazy[l] += lazy[pos];
lazy[r] += lazy[pos];

}

lazy[pos] = 0;
Expand Down
93 changes: 93 additions & 0 deletions Data structures/lazy_propagation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
vector<ll> v(N);
vector<ll> tree(4*N), lazy(4*N);

ll merge( ll left, ll right){
ll tmp;
// build temp from left and right according to quest
return tmp;
}

void ulazy( ll pos, ll lo, ll hi, ll qlo, ll qhi, ll val){

ll l = (pos<<1)+1, r = l+1;

if( lazy[pos] ){ // if there are pending updates to current node

// Here, add lazy[pos] effect to tree[pos](Current node)

// lazy propagation to child nodes if they exist
if( lo != hi ){

}

// clear lazy[pos]
lazy[pos] = 0;
}

if( lo > hi || lo > qhi || hi < qlo )
return;

if( qlo <= lo && hi <= qhi ){ // queried segment totally overlaps the current range of function

// add effect of entire range to tree[pos]

// Add lazy to child nodes if they exist
if( lo != hi ){

}
return;
}

ll mid = mid(lo,hi);

ulazy( l, lo, mid, qlo, qhi, val);
ulazy( r, mid+1, hi, qlo, qhi, val);

tree[pos] = merge( tree[l], tree[r] );

}

ll qlazy( ll pos, ll lo, ll hi, ll qlo, ll qhi){

ll l = (pos<<1)+1, r = l+1;

if( lo > qhi || lo > hi || hi < qlo )
return 0;

if( lazy[pos] ){ // if there are pending updates to current node
// Here, add lazy[pos] effect to tree[pos](Current node)

// lazy propagation to child nodes if they exist
if( lo != hi ){

}

lazy[pos] = 0;
}

if( qlo <= lo && qhi >= hi ) // the segment of current node is completely inside queried range
return tree[pos];

ll mid = mid( lo , hi );

if( qlo > mid)
return qlazy( r, mid+1, hi, qlo, qhi);
else if( qhi <= mid)
return qlazy( l, lo, mid, qlo, qhi);

ll left = qlazy( l, lo, mid, qlo, qhi);
ll right = qlazy( r, mid+1, hi, qlo, qhi);

return merge( left, right);
}


/*
BEFORE EVERY TEST CASE
lp(i, 4*n + 1){
lazy[i] = 0;
tree[i] = 0;
}
*/

0 comments on commit 83e8f1e

Please sign in to comment.