Lazy propagation
35 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)While a standard segment tree updates single points in O(log N) time, updating an entire range [L, R] naively modifies up to N leaves, degrading to O(N log N) time. Lazy propagation defers updates to restore O(log N) range updates.
Why Naive Range Updates Degrade to O(N log N)
A naive range update adds a value to every element of [L, R] by calling the point update once per index. To add 5 to a subarray of a standard segment tree, you must call update(i, val) for every index i from L to R (shown after the code).
void update(int i, int val) { /* point update: O(log N) */ }
void rangeAddNaive(int l, int r, int val) { for (int i = l; i <= r; i++) { update(i, val); // R - L + 1 calls, each O(log N) }}Updating up to N elements individually takes N * O(log N) operations. For 100,000 numbers and 100,000 range updates, this requires over 1.6 billion operations, causing severe time limit exceeded errors.
Notice that if an update completely covers a node's interval [start, end], you do not need to update its children immediately. You know the exact aggregated effect on the current node right away.
The Concept of Lazy Propagation
Lazy propagation delays updating children until they are actually needed. When a range update completely covers node interval [start, end], we update the node's stored value directly: tree[node] += val * (end - start + 1).
void update(int node, int start, int end, int l, int r, int val) { if (r < start || end < l) return; // no overlap if (l <= start && end <= r) { // node fully covered tree[node] += val * (end - start + 1); // fix this node's sum now lazy[node] += val; // children owe +val later return; // O(1): stop here } // partial overlap: push lazy[node] down, then recurse into both children}Instead of descending into its children, we mark the node with a lazy tag: lazy[node] += val. This tag records that children have pending updates that have not yet been applied.
The algorithm returns immediately in O(1) time at that node! If a future query or update never visits those children, those updates are never computed, saving massive amounts of work.
The PushDown Pattern for Child Traversal
What happens when a subsequent query or update needs to inspect a node's children? Before descending into child nodes, we must push the pending lazy tag down one level.
Example 1 · Range add by looping, brute force: O(N) per update and per query, O(1) space
public class PlainRangeAddDemo { static void rangeAdd(int[] arr, int left, int right, int value) { for (int i = left; i <= right; i++) arr[i] += value; // touch every element: O(N) }
static int rangeSum(int[] arr, int left, int right) { int sum = 0; for (int i = left; i <= right; i++) sum += arr[i]; return sum; }
public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Sum [0, 4]: " + rangeSum(arr, 0, 4)); rangeAdd(arr, 1, 3, 10); System.out.println("Sum [0, 4] after range add: " + rangeSum(arr, 0, 4)); System.out.println("Sum [1, 2]: " + rangeSum(arr, 1, 2)); }}Sum [0, 4]: 15 Sum [0, 4] after range add: 45 Sum [1, 2]: 25
What this code does
- rangeAdd() touches every element between left and right.
- rangeSum() walks the range too, so both operations cost O(N).
- It prints 15, 45 and 25, the same as the lazy tree.
Example 2 · Lazy segment tree, optimal: O(log N) range add and range sum, O(N) space
public class LazySegmentTreeDemo { private int[] tree, lazy; private int n;
public LazySegmentTreeDemo(int[] arr) { n = arr.length; tree = new int[4 * n]; lazy = new int[4 * n]; build(arr, 1, 0, n - 1); }
private void build(int[] arr, int node, int s, int e) { if (s == e) { tree[node] = arr[s]; return; } int mid = (s + e) / 2; build(arr, 2 * node, s, mid); build(arr, 2 * node + 1, mid + 1, e); tree[node] = tree[2 * node] + tree[2 * node + 1]; }
private void pushDown(int node, int s, int e) { if (lazy[node] != 0) { int mid = (s + e) / 2; int val = lazy[node]; // Left child tree[2 * node] += val * (mid - s + 1); lazy[2 * node] += val; // Right child tree[2 * node + 1] += val * (e - mid); lazy[2 * node + 1] += val; lazy[node] = 0; } }
public void updateRange(int l, int r, int val) { updateRange(1, 0, n - 1, l, r, val); }
private void updateRange(int node, int s, int e, int l, int r, int val) { if (l <= s && e <= r) { tree[node] += val * (e - s + 1); lazy[node] += val; return; } pushDown(node, s, e); int mid = (s + e) / 2; if (l <= mid) updateRange(2 * node, s, mid, l, r, val); if (r > mid) updateRange(2 * node + 1, mid + 1, e, l, r, val); tree[node] = tree[2 * node] + tree[2 * node + 1]; }
public int queryRange(int l, int r) { return queryRange(1, 0, n - 1, l, r); }
private int queryRange(int node, int s, int e, int l, int r) { if (l <= s && e <= r) return tree[node]; pushDown(node, s, e); int mid = (s + e) / 2; int sum = 0; if (l <= mid) sum += queryRange(2 * node, s, mid, l, r); if (r > mid) sum += queryRange(2 * node + 1, mid + 1, e, l, r); return sum; }
public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; LazySegmentTreeDemo lst = new LazySegmentTreeDemo(arr); System.out.println("Sum [0, 4]: " + lst.queryRange(0, 4)); lst.updateRange(1, 3, 10); // add 10 to indices 1..3 -> {1, 12, 13, 14, 5} System.out.println("Sum [0, 4] after range add: " + lst.queryRange(0, 4)); System.out.println("Sum [1, 2]: " + lst.queryRange(1, 2)); }}Sum [0, 4]: 15 Sum [0, 4] after range add: 45 Sum [1, 2]: 25
What this code does
- Sum [0, 4] originally = 1 + 2 + 3 + 4 + 5 = 15.
- Adding 10 to range [1, 3] adds 30 total: 15 + 30 = 45.
- Sum [1, 2] after update = 12 + 13 = 25. All range updates and queries execute in strictly O(log N) time.
- Why faster: a fully covered node stores a pending tag instead of touching its children, so a range update visits only about log2(N) nodes.
Example 3 · Lazy range assignment: O(log N) per operation
public class LazyAssignDemo { private int[] tree, lazy; private boolean[] hasLazy; private int n;
public LazyAssignDemo(int n) { this.n = n; tree = new int[4 * n]; lazy = new int[4 * n]; hasLazy = new boolean[4 * n]; }
private void push(int node, int s, int e) { if (hasLazy[node]) { int mid = (s + e) / 2; int v = lazy[node]; tree[2 * node] = v * (mid - s + 1); lazy[2 * node] = v; hasLazy[2 * node] = true;
tree[2 * node + 1] = v * (e - mid); lazy[2 * node + 1] = v; hasLazy[2 * node + 1] = true;
hasLazy[node] = false; } }
public void assign(int l, int r, int val) { assign(1, 0, n - 1, l, r, val); }
private void assign(int node, int s, int e, int l, int r, int val) { if (l <= s && e <= r) { tree[node] = val * (e - s + 1); lazy[node] = val; hasLazy[node] = true; return; } push(node, s, e); int mid = (s + e) / 2; if (l <= mid) assign(2 * node, s, mid, l, r, val); if (r > mid) assign(2 * node + 1, mid + 1, e, l, r, val); tree[node] = tree[2 * node] + tree[2 * node + 1]; }
public int query(int l, int r) { return query(1, 0, n - 1, l, r); }
private int query(int node, int s, int e, int l, int r) { if (l <= s && e <= r) return tree[node]; push(node, s, e); int mid = (s + e) / 2; int sum = 0; if (l <= mid) sum += query(2 * node, s, mid, l, r); if (r > mid) sum += query(2 * node + 1, mid + 1, e, l, r); return sum; }
public static void main(String[] args) { LazyAssignDemo st = new LazyAssignDemo(5); st.assign(0, 4, 1); // all 1s -> sum = 5 st.assign(1, 3, 5); // {1, 5, 5, 5, 1} -> sum = 17 System.out.println("Sum [0, 4]: " + st.query(0, 4)); System.out.println("Sum [0, 1]: " + st.query(0, 1)); }}Sum [0, 4]: 17 Sum [0, 1]: 6
What this code does
- Tracks pending assignments with boolean hasLazy flag.
- Overwrites node values and tags without recursion down to leaves.
- Evaluates in O(log N) time per range assignment.
Example 4 · Lazy tag state inspection
public class LazyTagInspectionDemo { static class SimpleLazyTree { int[] tree; int[] lazy; int n; SimpleLazyTree(int[] arr) { n = arr.length; tree = new int[4 * n]; lazy = new int[4 * n]; } void updateRange(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int node, int s, int e, int l, int r, int val) { if (lazy[node] != 0) { tree[node] += (e - s + 1) * lazy[node]; if (s != e) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } if (r < s || l > e) return; if (l <= s && e <= r) { tree[node] += (e - s + 1) * val; if (s != e) { lazy[2 * node] += val; lazy[2 * node + 1] += val; } return; } int mid = (s + e) / 2; update(2 * node, s, mid, l, r, val); update(2 * node + 1, mid + 1, e, l, r, val); tree[node] = tree[2 * node] + tree[2 * node + 1]; } int queryRange(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int node, int s, int e, int l, int r) { if (lazy[node] != 0) { tree[node] += (e - s + 1) * lazy[node]; if (s != e) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } if (r < s || l > e) return 0; if (l <= s && e <= r) return tree[node]; int mid = (s + e) / 2; return query(2 * node, s, mid, l, r) + query(2 * node + 1, mid + 1, e, l, r); } }
public static void main(String[] args) { int[] arr = {0, 0, 0, 0}; SimpleLazyTree lst = new SimpleLazyTree(arr); lst.updateRange(0, 3, 5); // root interval [0..3] absorbs tag directly System.out.println("Querying root [0, 3] directly: " + lst.queryRange(0, 3)); }}Querying root [0, 3] directly: 20
What this code does
- Root interval [0, 3] completely overlaps query [0, 3].
- Returns 20 in O(1) without visiting any child nodes.
- Demonstrates the power of lazy evaluation.
In the pushDown operation: 1) we apply the pending update to the left and right children's values, 2) we transfer the lazy tags to lazy[2 * node] and lazy[2 * node + 1], and 3) we clear lazy[node] = 0. Because updates and queries push lazy tags down only along the O(log N) nodes they actually visit, both range updates and range queries execute in strictly O(log N) time.
Order of execution
Order the push-down steps
Tap what happens at a node that only partly overlaps the update.
Next step
Check your understanding
10 quick questions