Algorithm Search⌘K

Proofs: Dynamic (sparse) segment tree

These are the human proofs of Dynamic (sparse) segment tree, at full length.

The dynamic segment tree keeps its invariant and answers range queries full

This proof shows: correctness, termination.

Let (M,⊕,e)(M, \oplus, e) be a commutative monoid, U≥1U \ge 1, and let T\mathcal T be the full segment tree over [0,U)[0, U): the root covers [0,U)[0, U), and a range [lo,hi)[lo, hi) with hi−lo≥2hi - lo \ge 2 has the children [lo,mid)[lo, mid) and [mid,hi)[mid, hi) with mid=⌊(lo+hi)/2⌋mid = \lfloor (lo + hi) / 2 \rfloor. Each node of the dynamic tree corresponds to one range of T\mathcal T, found by the walk from the root. Write A[lo,hi)=A[lo]⊕⋯⊕A[hi−1]A[lo, hi) = A[lo] \oplus \cdots \oplus A[hi - 1], with A[lo,lo)=eA[lo, lo) = e.

Invariant (D). (a) The created nodes form a subtree of T\mathcal T that contains the root. (b) Each created node vv over [lo,hi)[lo, hi) has val[v]=A[lo,hi)\mathit{val}[v] = A[lo, hi). (c) If a range of T\mathcal T has no created node, then A[k]=eA[k] = e for each kk in it.

At the start only the root exists with val=e\mathit{val} = e, and AA is all ee, so (D) holds.

Lemma 1 (termination of UPDATE). UPDATE(i,x)(i, x) with 0≤i<U0 \le i < U stops after d+1d + 1 passes of the loop, where d≤⌈lg⁡U⌉d \le \lceil \lg U \rceil is the depth of the leaf [i,i+1)[i, i+1) in T\mathcal T, and i∈[lo,hi)i \in [lo, hi) holds at each pass.

Proof. Initially i∈[0,U)i \in [0, U). If i<midi < mid, the new range is [lo,mid)∋i[lo, mid) \ni i, and otherwise it is [mid,hi)∋i[mid, hi) \ni i. Each pass replaces a range of length h≥2h \ge 2 by one of length ⌊h/2⌋\lfloor h/2 \rfloor or ⌈h/2⌉<h\lceil h/2 \rceil < h, so the length reaches 1 after at most ⌈lg⁡U⌉\lceil \lg U \rceil passes, and the range is then [i,i+1)[i, i+1). □\square

Lemma 2 (UPDATE keeps (D)). After UPDATE(i,x)(i, x) the invariant holds for the new array A′A' with A′[i]=A[i]⊕xA'[i] = A[i] \oplus x and A′[k]=A[k]A'[k] = A[k] for k≠ik \ne i.

Proof. By Lemma 1, the loop visits exactly the ranges of T\mathcal T that contain ii, from the root down to the leaf, and lines L7 and L10 create the missing ones, each as a child of a created node, so (a) holds. A node created at line L7 or L10 covers a range that had no node, so by (c) its items are all ee, and val=e=A[lo,hi)\mathit{val} = e = A[lo, hi) before line L3 adds xx. For each visited node, line L3 turns val[v]=A[lo,hi)\mathit{val}[v] = A[lo, hi) into A[lo,hi)⊕x=A′[lo,hi)A[lo, hi) \oplus x = A'[lo, hi), by commutativity and associativity, since i∈[lo,hi)i \in [lo, hi). A node that is not visited covers a range without ii, so its value is unchanged and still right. A range without a node after the update does not contain ii, since every range with ii now has a node, so (c) holds for A′A'. □\square

Lemma 3 (QUERY). For a created node vv over [lo,hi)[lo, hi), or v=0v = 0 for an absent range [lo,hi)[lo, hi) of T\mathcal T, and any 0≤l≤r≤U0 \le l \le r \le U, QUERY(v,lo,hi,l,r)(v, lo, hi, l, r) returns A[max⁡(l,lo),min⁡(r,hi))A[\max(l, lo), \min(r, hi)), read as ee when the range is empty.

Proof. By induction on hi−lohi - lo. If v=0v = 0, the range has no node, so by (c) every item in it is ee, and the result ee is right. If r≤lor \le lo or hi≤lhi \le l, the overlap is empty and ee is right. If l≤lol \le lo and hi≤rhi \le r, the overlap is [lo,hi)[lo, hi) and val[v]\mathit{val}[v] is right by (b). Otherwise the overlap is a proper part of [lo,hi)[lo, hi), so hi−lo≥2hi - lo \ge 2, and the node has the two child ranges. A missing child is passed as 0, which is covered by the first case. The overlap with [lo,hi)[lo, hi) is the overlap with [lo,mid)[lo, mid) followed by the overlap with [mid,hi)[mid, hi), so by the induction hypothesis and associativity line L15 returns the right value. □\square

Theorem. After any sequence of updates, QUERY(1,0,U,l,r)=A[l,r)(1, 0, U, l, r) = A[l, r) for 0≤l≤r≤U0 \le l \le r \le U, and the tree has at most min⁡(1+m⌈lg⁡U⌉,2U−1)\min(1 + m \lceil \lg U \rceil, 2U - 1) nodes after mm updates.

Proof. The first part follows from Lemma 2 by induction over the updates and from Lemma 3 with the root, since [max⁡(l,0),min⁡(r,U))=[l,r)[\max(l, 0), \min(r, U)) = [l, r). For the size, each update creates nodes only on one path of T\mathcal T below the root, at most ⌈lg⁡U⌉\lceil \lg U \rceil of them, and all created nodes are distinct nodes of T\mathcal T, which has 2U−12U - 1 nodes. □\square

Back to Dynamic (sparse) segment tree