Plenty of problems ask you to answer the same kind of question over and over against data that keeps changing: what’s the sum of this range right now, what’s the smallest value in that range right now, given that the underlying array has been getting point updates in between queries.
Plenty of problems ask you to answer the same kind of question over and over against data that keeps changing: what’s the sum of this range right now, what’s the smallest value in that range right now, given that the underlying array has been getting point updates in between queries. A single pass gives you the answer once; recomputing that pass after every update is correct but slow. this article covers the two workhorse structures built specifically for that situation — Fenwick trees (also called binary indexed trees) and segment trees — both of which answer range queries and apply updates in logarithmic time instead of linear time, by organizing the array into a hierarchy of overlapping ranges rather than storing it flat.
The signal for reaching into this article is a problem that interleaves reads and writes over the same array-like data: “process a stream of updates and queries,” “support point updates and range-sum queries,” “the values change over time and I need an answer after each change.” That’s fundamentally different from every prefix-sum trick you might reach for in a static array — a plain running-prefix-sum array answers range-sum queries in O(1), but a single update anywhere forces an O(n) rebuild of everything after it, which is a dealbreaker once updates and queries are interleaved at scale. A second signal is a range query that isn’t sum — minimum, maximum, GCD — over a mutable array; the general shape of a segment tree adapts to almost any associative combining operation, not just addition. A third, more specific signal is “count something across an array using the relative order of elements,” which is what Fenwick trees are unreasonably good at, as this article’s capstone will show.
These structures don’t apply when the array is genuinely static (no future updates) — a plain prefix-sum array is simpler and asymptotically just as fast for that case, so reach for one of these only once mutation enters the picture. They’re also usually overkill for a single query with no updates, or for a small, fixed number of updates where a straightforward recompute is fast enough and easier to get right under interview time pressure. Fenwick trees, specifically, are the leaner, simpler-to-code choice when your operation is sum (or anything invertible, like XOR) — reach for the more general segment tree when you need range updates in addition to range queries, or when your combining operation isn’t invertible, like minimum or maximum.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithmic Edge.
Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
