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
- Adding a value to every item of a range one by one costs O(n). Lazy propagation makes it O(log n).
- 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".
- The tag is pushed down to the children only when a later update or query must look inside that node.
- Queries use the same rule, so both operations stay O(log n).
Watch it run
Loading animation…