Proofs: Segment tree
These are the human proofs of Segment tree, at full length.
The operations of a segment tree keep the node invariant and return the range product full
This proof shows: correctness, termination.
Let be a monoid and let . For integers we write when , and when . By associativity, does not depend on the brackets, and for ,
For this is associativity; when one side is empty it is the identity law. The range of a node is the pair that the calls pass with it. The invariant is
(S) for each node of range : .
Lemma 0 (the calls are well formed). Each call BUILD, QUERY or UPDATE that starts from has . If , the two child ranges and are nonempty, they are adjacent, their union is , and each is shorter than .
Proof. The first call has . If , then , so and . Both child lengths, and , are at least 1 and at most . By induction on the depth of the call, every call has a nonempty range inside .
Termination. Each recursive call is on a child range, whose length is smaller by at least 1 than that of the caller, by Lemma 0. A call with makes no recursive call in BUILD (line L1) or UPDATE (line L10). In QUERY, a call with returns in line L6 or line L7, since the single index is either outside or inside it. So every call ends.
Lemma 1 (build). After BUILD, (S) holds for and for every node below .
Proof. By strong induction on . If , line L1 sets , and has no node below it. If , lines L3 and L4 build the two children; their lengths are smaller, so by the induction hypothesis and , and (S) holds below them. The two subtrees use disjoint node indices, because the indices below start with the bits of and those below start with the bits of , so the second call does not change a cell that the first one set. Line L5 then sets by (1).
Lemma 2 (query). Assume (S). Then QUERY returns , that is, the product of over in left to right order, and when the intersection is empty. It does not change .
Proof. By strong induction on . Write and .
- If or , the intersection is empty, so and the answer is , which line L6 returns.
- Otherwise, if and , then and , and line L7 returns by (S).
- Otherwise , since a range of one index is either disjoint from or inside it. By Lemma 0 the children have shorter ranges, so by the induction hypothesis the two calls of line L9 return and . We show that their product is . Let , the last index of the intersection that is at most , or if there is none. The first value equals and the second equals : if and , then and both are clear; if , then , the first value is and the second range is empty, so it gives ; if , then , the first range is empty and the second is . In each case , so by (1) the product of line L9 is , with the left value first.
QUERY only reads .
The top call QUERY with therefore returns .
Lemma 3 (update). Assume (S) before UPDATE with , and let be with . Afterwards (S) holds for .
Proof. We show, by strong induction on , that if and (S) holds for at every node below and at , then after UPDATE the statement (S) holds for at and at every node below . If , then and line L10 sets . If , then lies in exactly one child range, by Lemma 0: the left one when and the right one otherwise, which is the test of lines L12 and L13. By the induction hypothesis, after the call that child and all nodes below it satisfy (S) for . The other child does not hold in its range, so its subtree satisfies (S) for , and and agree on its range, so it satisfies (S) for as well; the call did not touch its cells, because they lie in the other subtree. Line L14 sets by (1) for .
At the top call, every node of the tree is the root or below it. The nodes that the call does not reach have a range without , and they keep (S) by the same argument.
Theorem. After BUILD and any sequence of updates, each query with returns for the current array, and every call ends.
Proof. Lemma 1 gives (S) after BUILD. Lemma 3 keeps (S) across each update, and Lemma 2 shows that a query does not change and returns the right value under (S). Termination was shown above.