Algorithm Search⌘K

Proofs: Segment tree beats

These are the human proofs of Segment tree beats, at full length.

Segment tree beats keeps the capped node fields and returns correct sums and maximums full

This proof shows: correctness, termination.

We use the notation of the page. For a node vv with range I(v)I(v) and an item i∈I(v)i \in I(v), the seen value is sv(i)=mx1[v]s_v(i) = \mathit{mx1}[v] if vv is a leaf, and sv(i)=min⁡(sc(i),mx1[v])s_v(i) = \min(s_c(i), \mathit{mx1}[v]) otherwise, where cc is the child of vv whose range holds ii. For a finite multiset XX of numbers, let fields(X)\mathrm{fields}(X) be the tuple (max⁡X,max⁡{y∈X:y<max⁡X},∣{y∈X:y=max⁡X}∣,∑X)(\max X, \max\{y \in X : y < \max X\}, |\{y \in X : y = \max X\}|, \sum X), with max⁡∅=−∞\max \emptyset = -\infty. Write Xv={sv(i):i∈I(v)}X_v = \{ s_v(i) : i \in I(v) \} as a multiset. The invariant is

(B)(mx1,mx2,cnt,sum)[v]=fields(Xv) for every node v,and A[i]=s1(i) for every i.\text{(B)} \qquad (\mathit{mx1}, \mathit{mx2}, \mathit{cnt}, \mathit{sum})[v] = \mathrm{fields}(X_v) \text{ for every node } v, \quad \text{and } A[i] = s_1(i) \text{ for every } i.

Besides (B) we keep a second invariant, which says that a pending cap only ever cuts the copies of the maximum of a child:

(C)for every inner node v and child c of v: if mx1[c]>mx1[v], then mx2[c]<mx1[v].\text{(C)} \qquad \text{for every inner node } v \text{ and child } c \text{ of } v: \text{ if } \mathit{mx1}[c] > \mathit{mx1}[v] \text{, then } \mathit{mx2}[c] < \mathit{mx1}[v].

Lemma 1 (pull). If vv has children aa and bb whose fields are fields(Ya)\mathrm{fields}(Y_a) and fields(Yb)\mathrm{fields}(Y_b) for multisets YaY_a, YbY_b, then after PULL(v)(v) the fields of vv are fields(Ya⊎Yb)\mathrm{fields}(Y_a \uplus Y_b), and (C) holds for vv and its two children.

Proof. The sum of a disjoint union is the sum of the sums (line L5). Let Ma=max⁡YaM_a = \max Y_a and Mb=max⁡YbM_b = \max Y_b. The maximum of the union is max⁡(Ma,Mb)\max(M_a, M_b). If Ma=MbM_a = M_b, the copies of the maximum are those of both sides, and a value below the maximum is a value of one side below its maximum, so the strict second maximum is the larger of the two second maximums (line L6). If Ma>MbM_a > M_b, the copies are those of aa only, and the values below the maximum are those of aa below MaM_a together with all of YbY_b, whose largest is MbM_b, so the strict second maximum is max⁡(mx2[a],Mb)\max(\mathit{mx2}[a], M_b) (line L7). Line L8 is the mirror case. Now mx1[v]≥mx1[a],mx1[b]\mathit{mx1}[v] \ge \mathit{mx1}[a], \mathit{mx1}[b], so the premise of (C) is false for both children. □\square

Lemma 2 (apply). Let (B) and (C) hold, let vv be a node whose parent pp, if any, has mx1[p]≥mx1[v]\mathit{mx1}[p] \ge \mathit{mx1}[v], and let mx2[v]<x\mathit{mx2}[v] < x. After APPLY(v,x)(v, x), the fields of vv are fields({min⁡(y,x):y∈Xv})\mathrm{fields}(\{ \min(y, x) : y \in X_v \}), and (C) still holds.

Proof. If x≥mx1[v]=max⁡Xvx \ge \mathit{mx1}[v] = \max X_v, nothing changes. Otherwise mx2[v]<x<mx1[v]\mathit{mx2}[v] < x < \mathit{mx1}[v]. The items of XvX_v above xx are exactly the cnt[v]\mathit{cnt}[v] copies of max⁡Xv\max X_v, since every other item is at most mx2[v]<x\mathit{mx2}[v] < x. Capping lowers each copy by mx1[v]−x\mathit{mx1}[v] - x, which line L9 subtracts from the sum. The new maximum is xx with the same count, and the values below it are unchanged, with the largest still mx2[v]<x\mathit{mx2}[v] < x.

For (C) between pp and vv: mx1[v]\mathit{mx1}[v] only goes down, so it stays at most mx1[p]\mathit{mx1}[p]. For (C) between vv and a child cc, assume mx1[c]>x\mathit{mx1}[c] > x; we show mx2[c]<x\mathit{mx2}[c] < x. If mx2[c]=−∞\mathit{mx2}[c] = -\infty this holds. Otherwise let y=mx2[c]y = \mathit{mx2}[c], a value of XcX_c. If mx1[c]≤\mathit{mx1}[c] \le the old mx1[v]\mathit{mx1}[v], the cap of vv did not cut XcX_c, so yy is a value of XvX_v below max⁡Xc≤max⁡Xv\max X_c \le \max X_v, hence y≤mx2[v]<xy \le \mathit{mx2}[v] < x. If mx1[c]>\mathit{mx1}[c] > the old mx1[v]\mathit{mx1}[v], then by (C) y<y < the old mx1[v]\mathit{mx1}[v], so yy is not cut, it is a value of XvX_v below max⁡Xv\max X_v, and again y≤mx2[v]<xy \le \mathit{mx2}[v] < x. □\square

Lemma 3 (push). Let (B) and (C) hold, and let vv be an inner node with s1(i)=sv(i)s_1(i) = s_v(i) for i∈I(v)i \in I(v). After PUSH(v)(v), (B) and (C) hold, no value su(i)s_u(i) at vv or above has changed, and each child cc has mx1[c]≤mx1[v]\mathit{mx1}[c] \le \mathit{mx1}[v], so that s1(i)=sc(i)s_1(i) = s_c(i) for i∈I(c)i \in I(c).

Proof. Let cc be a child and x=mx1[v]x = \mathit{mx1}[v]. If mx1[c]≤x\mathit{mx1}[c] \le x, line L9 does nothing. Otherwise (C) gives mx2[c]<x\mathit{mx2}[c] < x, and mx1[c]>x\mathit{mx1}[c] > x is not at most mx1[v]\mathit{mx1}[v], so we cannot use Lemma 2 for cc as stated; but its proof of the fields and of (C) toward the children of cc uses only mx2[c]<x\mathit{mx2}[c] < x, so the fields of cc become those of {min⁡(y,x):y∈Xc}\{ \min(y, x) : y \in X_c \}, which is XvX_v on I(c)I(c), and (C) holds below cc. Now mx1[c]≤x=mx1[v]\mathit{mx1}[c] \le x = \mathit{mx1}[v], so (C) holds between vv and cc. The new sc(i)s_c(i) equals the old sv(i)s_v(i), and sv(i)=min⁡(sc(i),x)s_v(i) = \min(s_c(i), x) is unchanged, so no value at vv or above changes and (B) holds. Since mx1[c]≤mx1[v]\mathit{mx1}[c] \le \mathit{mx1}[v], the cap of vv does not cut scs_c, and s1(i)=sv(i)=sc(i)s_1(i) = s_v(i) = s_c(i) on I(c)I(c). □\square

Lemma 4 (chmin). Let (B) and (C) hold, and call CHMIN(v,lo,hi,l,r,x)(v, lo, hi, l, r, x) on a node vv with I(v)=[lo,hi]I(v) = [lo, hi] such that max⁡Xv\max X_v is not above the cap of any ancestor, that is, s1(i)=sv(i)s_1(i) = s_v(i) for i∈I(v)i \in I(v). After the call, (B) and (C) hold, (B) for the array A′A' with A′[i]=min⁡(A[i],x)A'[i] = \min(A[i], x) for i∈[l,r]∩I(v)i \in [l, r] \cap I(v) and A′[i]=A[i]A'[i] = A[i] otherwise, and the fields of vv are fields\mathrm{fields} of the new values of I(v)I(v).

Proof. By induction on hi−lohi - lo. If line L11 returns because I(v)I(v) misses [l,r][l, r], nothing changes, which is right. If it returns because mx1[v]≤x\mathit{mx1}[v] \le x, every value of I(v)I(v) is at most xx and the chmin does nothing on it. If line L12 applies, I(v)⊆[l,r]I(v) \subseteq [l, r], the parent of vv was pushed before the call so that mx1\mathit{mx1} of the parent is at least mx1[v]\mathit{mx1}[v], and Lemma 2 gives the fields of {min⁡(y,x):y∈Xv}\{ \min(y, x) : y \in X_v \} and keeps (C); the seen values sv(i)s_v(i) become min⁡(sv(i),x)\min(s_v(i), x), which by the assumption on the ancestors are the new A′[i]A'[i]; the caps of the ancestors are at least the old max⁡Xv>x\max X_v > x, so they still do not cut. Otherwise vv is not a leaf, because a leaf has mx2=−∞<x\mathit{mx2} = -\infty < x and a leaf that meets [l,r][l, r] lies inside it. Line L13 pushes, which by Lemma 3 keeps (B) and (C) and makes each child satisfy the assumption of the lemma. By the hypothesis, the two calls of line L14 update the children correctly, and they do not touch each other's subtrees. Then XvX_v is again {min⁡(sc(i),mx1[v])}\{ \min(s_c(i), \mathit{mx1}[v]) \}, but the children may now have smaller maximums, so line L15 recomputes the fields of vv from the children by Lemma 1, which also restores (C) for vv. After this, mx1[v]\mathit{mx1}[v] is the maximum of the children, so it caps nothing, and the fields of vv are those of the new values of I(v)I(v). □\square

Lemma 5 (query). Under the same assumption on vv as in Lemma 4, QUERY-SUM(v,lo,hi,l,r)(v, lo, hi, l, r) returns ∑i∈[l,r]∩I(v)A[i]\sum_{i \in [l, r] \cap I(v)} A[i], and leaves (B) and (C) true. The same holds for QUERY-MAX with max⁡\max and −∞-\infty.

Proof. By induction on hi−lohi - lo. Lines L16 and L17 return the empty sum or, by (B) and the assumption, the sum of the true values of I(v)I(v). Otherwise line L18 pushes, which keeps (B) and (C) and gives each child the assumption, by Lemma 3, and the two recursive results add up to the answer. □\square

Termination. Each recursive call of BUILD, CHMIN and QUERY is on a child, whose range is strictly shorter, and no call recurses on a leaf: in CHMIN, a leaf either meets line L11 or satisfies line L12, as shown in Lemma 4. PULL, APPLY and PUSH do O(1)O(1) work.

Theorem. After BUILD on AA and any sequence of operations, CHMIN(1,0,n−1,l,r,x)(1, 0, n - 1, l, r, x) performs the chmin on [l,r][l, r], and QUERY-SUM and QUERY-MAX at the root return the sum and the maximum of A[l…r]A[l \ldots r] for the current array.

Proof. BUILD establishes (B) and (C) by Lemma 1, with no caps cutting. The root has no ancestors, so Lemmas 4 and 5 apply to the root, and they keep (B) and (C). Induction over the sequence of operations gives the claim, and each call ends by the termination argument. ■\blacksquare

Back to Segment tree beats