Foundations — Segment tree — build, range query, point update
This page assumes you know nothing about segment trees. Every letter, bracket, and arrow the parent note uses is built here, one at a time, each resting on the previous.
0. The array — the thing we are storing
Look at the figure: the box labelled a[0] holds 2, the box a[2] holds 5. The little number under each box (0, 1, 2, 3) is its index; the number inside is its value.

- The symbol
a[i]is read "a at i" — the value stored at indexi. nis the number of boxes (heren = 4), so the valid indices run0, 1, ..., n-1.
1. A range [lo, hi] — a contiguous slice
The picture below shades the range in red. Notice it is contiguous — no gaps. Segment trees only ever deal with contiguous ranges; that is what makes the cut-in-half trick work.

- = the lowest index in the slice.
- = the highest index in the slice.
- The parent question "sum of
a[l..r]?" useslandras the query's low and high. Same idea, different letters:lis a lo,ris a hi. - A range with (like ) is a single box — the smallest possible range.
2. Splitting a range — the midpoint
Two new symbols to earn here:
- is the floor — "round down to the nearest whole number". So . We need it because can be a fraction, but an index must be a whole number.
- The two halves never overlap and cover everything: the left ends at , the right starts at . No box is counted twice, none is left out.

3. The logarithm — how many halvings?
- The parent writes for query and update speed. That is exactly the number of levels in the tree — the number of halvings from the whole array down to one box.
- is the ceiling — "round up". The tree's height is because a fractional number of levels still needs a whole extra level.
4. A binary tree — halves inside halves
Map this straight onto ranges:
- The root is the whole range .
- Each node's two children are its left half and right half.
- A leaf is a single box — it cannot split further.
So the tree is the family history of splits from Section 2. Nothing new — just a name for the picture of halves-within-halves.
5. Heap indexing — a tree living in a flat array
We do not build the tree with pointers. We store it in one flat array tree[] and use arithmetic to move around it. This trick comes from the Heap.

vis just the name of a node = its slot number intree[]. When the code saysbuild(2*v, ...)it means "go handle my left child."tree[v]is the stored answer for whatever range nodevcovers.
6. Combine and identity — gluing two answers
- The parent's "no overlap → return 0" returns this identity, so an empty contribution safely folds into the total.
- For min the identity is ; for max it is ; for product it is
1. Same skeleton, different glue — which is why the topic keeps saying "identity" rather than literally "0".
7. Big-O notation — measuring cost
The whole point of the topic is trading a slow cost for a fast one:
| Operation | Naive array | Segment tree |
|---|---|---|
| Range query | ||
| Point update |
Compare with cousins: Prefix Sum gives queries but updates; Fenwick Tree (BIT) matches the segment tree's / with less code but less flexibility; Lazy Propagation extends the tree to fast range updates.
The prerequisite map
Equipment checklist
Read a[i] aloud and say what index and value mean
i, where positions count from 0.State how many boxes a size-n array has and its valid indices
n boxes, indices 0 through n-1.Say what the range contains
lo to hi inclusive — a contiguous slice.Compute for and give both halves
Explain why the two halves never overlap
mid, right starts at mid+1, so no index is shared and none is skipped.Define and
Answer "what is ?" in one sentence
n to reach 1.Give the heap indices of the two children of node v and of the root
2v and 2v+1; root at index 1.Say why we allocate tree[] of size
n can reach nearly 4n; 4n is a safe over-allocation.Give the identity element for sum, min, and max
0 for sum, for min, for max.