Search code examples
algorithmdata-structuresclrsinterval-tree

Lazy propagation for Segmented Tree


There is something unclear to me in the lazy propagation algorithm for segmented trees. According to the code below, upon completely overlap of the query interval, the update value is just added to the parent node and the children are marked for lazy updates. But as you can see in the image attached, if an update is done +4 for range 0,1 the result is totally different in both trees! (left image: no lazy propagation).

void update_tree(int node, int a, int b, int i, int j, int value) {
if(lazy[node] != 0) { // This node needs to be updated
    tree[node] += lazy[node]; // Update it

    if(a != b) {
        lazy[node*2] += lazy[node]; // Mark child as lazy
            lazy[node*2+1] += lazy[node]; // Mark child as lazy
    }

    lazy[node] = 0; // Reset it
}

if(a > b || a > j || b < i) // Current segment is not within range [i, j]
    return;

if(a >= i && b <= j) { // Segment is fully within range
        tree[node] += value;

    if(a != b) { // Not leaf node
        lazy[node*2] += value;
        lazy[node*2+1] += value;
    }

        return;
}

update_tree(node*2, a, (a+b)/2, i, j, value); // Updating left child
update_tree(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child

tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value}

enter image description here

So the question is, what if a query asking for the sum from 0,1 was invoked after the +4 update?


Solution

  • Ok, I've found a the correct way to update the segment tree with lazy propagation for sums in that link.

    Lazy propagation for sums