Data Structures19 sections · 729 units
Open in Course

Segment Tree for Intervals

Range queries on intervals

Sometimes interval queries map to segment tree operations.

Given nn intervals on a range [0,M][0, M], answer queries like:

  • "How many intervals cover point xx?"
  • "What's the maximum overlap in range [l,r][l, r]?"

Build a segment tree on positions 00 to MM.

For each interval [l,r][l, r], do a range update: add 1 to all positions from ll to rr. To count intervals covering point xx: point query at xx.

This approach works when MM is small enough. For large MM, use coordinate compression.