Skip to content
StackDecode
Advanced data structuresAnimated

Lazy Propagation

Delay range updates in the tree until they are needed.

Time

O(log n) range update

Extra space

O(n)

Use it for

Range updates plus range queries.

How it works

  1. Adding a value to every item of a range one by one costs O(n). Lazy propagation makes it O(log n).
  2. When an update range fully covers a node, update only that node: add d × (its length) to its sum and leave a "lazy tag" +d meaning "my children still owe this".
  3. The tag is pushed down to the children only when a later update or query must look inside that node.
  4. Queries use the same rule, so both operations stay O(log n).

Watch it run

Loading animation…