Data Structures19 sections · 729 units
Open in Course

Intro

Why segment trees matter

Segment trees answer range queries in O(logn)O(\log n). Structure: Binary tree where each node covers a range. Leaves are single elements.

Internal nodes combine children. Operations:

  • Build: O(n)O(n)
  • Query: O(logn)O(\log n)
  • Update: O(logn)O(\log n)

Use Cases: Range sum, range min/max, range GCD.

When you need both updates and queries. vs Prefix Sum: Prefix sum is O(1)O(1) query but O(n)O(n) update. Segment tree is O(logn)O(\log n) for both. Space: O(n)O(n).