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 with range and an item , the seen value is if is a leaf, and otherwise, where is the child of whose range holds . For a finite multiset of numbers, let be the tuple , with . Write as a multiset. The invariant is
Besides (B) we keep a second invariant, which says that a pending cap only ever cuts the copies of the maximum of a child:
Lemma 1 (pull). If has children and whose fields are and for multisets , , then after PULL the fields of are , and (C) holds for and its two children.
Proof. The sum of a disjoint union is the sum of the sums (line L5). Let and . The maximum of the union is . If , 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 , the copies are those of only, and the values below the maximum are those of below together with all of , whose largest is , so the strict second maximum is (line L7). Line L8 is the mirror case. Now , so the premise of (C) is false for both children.
Lemma 2 (apply). Let (B) and (C) hold, let be a node whose parent , if any, has , and let . After APPLY, the fields of are , and (C) still holds.
Proof. If , nothing changes. Otherwise . The items of above are exactly the copies of , since every other item is at most . Capping lowers each copy by , which line L9 subtracts from the sum. The new maximum is with the same count, and the values below it are unchanged, with the largest still .
For (C) between and : only goes down, so it stays at most . For (C) between and a child , assume ; we show . If this holds. Otherwise let , a value of . If the old , the cap of did not cut , so is a value of below , hence . If the old , then by (C) the old , so is not cut, it is a value of below , and again .
Lemma 3 (push). Let (B) and (C) hold, and let be an inner node with for . After PUSH, (B) and (C) hold, no value at or above has changed, and each child has , so that for .
Proof. Let be a child and . If , line L9 does nothing. Otherwise (C) gives , and is not at most , so we cannot use Lemma 2 for as stated; but its proof of the fields and of (C) toward the children of uses only , so the fields of become those of , which is on , and (C) holds below . Now , so (C) holds between and . The new equals the old , and is unchanged, so no value at or above changes and (B) holds. Since , the cap of does not cut , and on .
Lemma 4 (chmin). Let (B) and (C) hold, and call CHMIN on a node with such that is not above the cap of any ancestor, that is, for . After the call, (B) and (C) hold, (B) for the array with for and otherwise, and the fields of are of the new values of .
Proof. By induction on . If line L11 returns because misses , nothing changes, which is right. If it returns because , every value of is at most and the chmin does nothing on it. If line L12 applies, , the parent of was pushed before the call so that of the parent is at least , and Lemma 2 gives the fields of and keeps (C); the seen values become , which by the assumption on the ancestors are the new ; the caps of the ancestors are at least the old , so they still do not cut. Otherwise is not a leaf, because a leaf has and a leaf that meets 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 is again , but the children may now have smaller maximums, so line L15 recomputes the fields of from the children by Lemma 1, which also restores (C) for . After this, is the maximum of the children, so it caps nothing, and the fields of are those of the new values of .
Lemma 5 (query). Under the same assumption on as in Lemma 4, QUERY-SUM returns , and leaves (B) and (C) true. The same holds for QUERY-MAX with and .
Proof. By induction on . Lines L16 and L17 return the empty sum or, by (B) and the assumption, the sum of the true values of . 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.
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 work.
Theorem. After BUILD on and any sequence of operations, CHMIN performs the chmin on , and QUERY-SUM and QUERY-MAX at the root return the sum and the maximum of 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.