Segment Trees and Lazy Range Updates

Represent intervals hierarchically and defer child updates while keeping every node’s aggregate correct.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Split the range into a logarithmic number of pieces
  2. A lazy tag records work still owed to descendants

Split the range into a logarithmic number of pieces

A segment tree stores an aggregate for every node interval. A range query descends only into partially overlapping nodes and uses the stored value directly for complete coverage. At any depth, at most two intervals are cut by the query boundaries. This yields \(O(\log n)\) selected nodes and partial-level work for a contiguous query.

A lazy tag records work still owed to descendants

For range addition and range sum, adding \(a\) to a covered interval of length \(k\) increases its sum by \(ak\). Save an additional pending \(a\) at the node. When a later query or update needs its children, push that tag down, updating both child sums and tags before clearing the parent’s. With operations such as assignment and addition, order of tag composition matters. Fenwick trees give a compact alternative for point updates and prefix sums. Rollback connectivity uses the same interval decomposition over time.[1]

References

  1. MIT OpenCourseWare, Design and Analysis of Algorithms: Lecture Notes ↩