Algorithm · Data Structures

Ranges in Log Time.

Don't memorize the segment tree — watch it answer ranges in log time. Prefix sums give O(1) range queries but choke on updates. A segment tree fixes that: each node stores the sum of an interval, so a range query combines just a handful of covering nodes in O(log n) — and changing one value fixes a single leaf and ripples up to the root, also O(log n).

interval sums · range query · point update · O(log n)
NODE = INTERVAL

Every node stores the sum of a contiguous slice of the array.

LEAVES

The array elements themselves — the base of the tree.

QUERY

Combine the few nodes that fully cover the range — O(log n).

UPDATE

Change a leaf, recompute its ancestors up to the root — O(log n).

segment-tree.sim — array [2,1,5,3] · build · query · update
Ready

Leaves hold the array [2,1,5,3]; each parent stores the sum of its interval. Press Step to build the tree bottom-up, then answer a range query, then update a value and watch it ripple to the root.

Build
Phase
Result

Precompute the pieces, combine on demand

A prefix-sum array is unbeatable for range sums until a value changes — then every prefix after it is wrong, an O(n) repair. The segment tree trades that O(1) query for a still-fast O(log n) query in exchange for O(log n) updates, by storing partial sums at many granularities. No single node covers your exact range, but a logarithmic number of them tile it perfectly, and any single element sits under only a logarithmic number of nodes.

01

Build bottom-up

Leaves are the elements; each internal node is the sum of its two children. Building the whole tree is O(n).

02

Query a range

Walk down from the root: take a whole node if its interval lies inside the query, recurse where it partially overlaps. About 2 log n nodes.

03

Update a point

Change the leaf, then recompute each ancestor from its children up to the root — only log n nodes change.

04

Range updates

Lazy propagation defers pushing an update to children until needed, giving O(log n) range updates too.

Build
O(n)
Range query
O(log n)
Point update
O(log n)
Space
O(n)

The same tree works for any associative combine — swap sum for min, max, or gcd and you get range-min or range-gcd queries for free. Lighter alternatives exist: a Fenwick (binary indexed) tree does range-sum with updates in a few lines and less memory, while a prefix-sum array wins only when the data never changes.

Check yourself

1 · What does each internal node of a segment tree store?

Internal nodes hold the combination of their children, so they summarize a whole interval of the array.

2 · Why can a segment tree update a value in O(log n)?

A single element sits under a logarithmic number of nodes, so only those ancestors need recomputing.

3 · Compared to a prefix-sum array, a segment tree is better when…

Prefix sums give O(1) queries but O(n) updates; the segment tree keeps updates fast (O(log n)) for mutable data.

Questions

How much memory does a segment tree use?

About 2n to 4n nodes for an array of n elements (a common safe allocation is 4n). Each node stores one aggregate value, so it is O(n) space overall.

What is lazy propagation?

A technique for range updates: instead of updating every affected leaf, you mark a node as "pending +x for my whole interval" and only push that down to children when a later query or update needs to descend. It keeps range updates at O(log n).

When would I pick a Fenwick tree instead?

When you only need prefix or range sums with point updates, a Fenwick (binary indexed) tree is shorter to write and uses less memory. Reach for a segment tree when you need range updates, non-sum operations, or more flexibility.

Both ways, in log time.

Store interval sums in a tree and range queries and point updates both cost O(log n). The mutable cousin of the prefix sum.

Finished this one? 0 / 47 Algorithms done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Algorithms