When to Use

Pattern triggers

Look for these signals in the problem:

  • "Range sum/min/max query" with updates: Segment tree handles both in O(logn)O(\log n).
  • "Count elements" in range satisfying condition: Segment tree with custom aggregation function.
  • "Update range, query point": Lazy propagation variant handles range updates.
  • "Merge sort based" counting: Segment tree on value space counts inversions, smaller numbers, etc.
  • Dynamic order statistics: Segment tree tracks count of elements for rank queries.