Data Structures19 sections · 729 units
Open in Course

Lazy Propagation Structure

The lazy array

Add a lazy array alongside the tree array:

class LazySegmentTree:
    function init(n):
        this.n = n
        this.tree = array of 4*n zeros
        this.lazy = array of 4*n zeros

lazy[node] stores pending updates that haven't been propagated to children yet.

For "add vv to range": lazy[node] means "add vv to all elements in this node's range."

Before accessing a node's children, always push down any lazy value first. This guarantees correctness.