← capítulo

Segment trees

Range query AND point update, both O(log n). Refuse the array's "fast query OR fast update" choice.

A tree of range aggregates

Watch a query decompose

Sum of [2:6] = 3 canonical segments (green), not 5 element reads.

Wins on mixed workloads

50/50 query/update: segment tree 6× faster than raw array, 100× faster than prefix sums.

When (and when not)

Takeaway

Wins by having no slow operation. Benchmark the workload, not one op. Next: the Fenwick tree — the lean sum specialist.