3.4.15 · D1Trees

Foundations — Segment tree — build, range query, point update

1,822 words8 min readBack to topic

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.

Figure — Segment tree — build, range query, point update
  • The symbol a[i] is read "a at i" — the value stored at index i.
  • n is the number of boxes (here n = 4), so the valid indices run 0, 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.

Figure — Segment tree — build, range query, point update
  • = the lowest index in the slice.
  • = the highest index in the slice.
  • The parent question "sum of a[l..r]?" uses l and r as the query's low and high. Same idea, different letters: l is a lo, r is 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.
Figure — Segment tree — build, range query, point update

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.

Figure — Segment tree — build, range query, point update
  • v is just the name of a node = its slot number in tree[]. When the code says build(2*v, ...) it means "go handle my left child."
  • tree[v] is the stored answer for whatever range node v covers.

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

Array and index a of i

Range lo to hi

Split at midpoint mid

Divide and Conquer

Binary tree of halves

Logarithm log base 2 of n

Heap indexing 2v and 2v+1

Combine and identity element

Segment tree

Big O cost


Equipment checklist

Read a[i] aloud and say what index and value mean
"a at i" — the value stored at position 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
Every index from lo to hi inclusive — a contiguous slice.
Compute for and give both halves
; halves are and .
Explain why the two halves never overlap
Left ends at mid, right starts at mid+1, so no index is shared and none is skipped.
Define and
Floor rounds down, ceiling rounds up, to the nearest whole number.
Answer "what is ?" in one sentence
The number of times you halve n to reach 1.
Give the heap indices of the two children of node v and of the root
Children 2v and 2v+1; root at index 1.
Say why we allocate tree[] of size
Heap indexing on non-power-of-2 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.
Explain what means in plain words
Work grows by only one step each time the input doubles — very slow growth.