Proofs: Persistent segment tree
These are the human proofs of Persistent segment tree, at full length.
The persistent segment tree keeps every version and answers sums and ranks correctly full
This proof shows: correctness.
Fix . For a node and a range with , say that is well formed for if either and is a leaf, or , is an inner node, is well formed for , is well formed for , and , where . For such a , define the array by for a leaf, and otherwise. Since when , both ranges are shorter, so the definitions are by induction on .
Lemma 1. If is well formed for , then is the sum of .
Proof. By induction on . For a leaf both sides are . For an inner node, , and by induction these are the sums of the two halves of .
Lemma 2 (no change). No procedure writes a field of an existing node. Hence, if is well formed for at some moment, it stays well formed for and never changes.
Proof. BUILD and UPDATE only call NEW-NODE, which writes the fields of a node that did not exist before, and QUERY, GET and KTH only read. Well formedness and depend only on the fields of the nodes reachable from , and none of them changes.
Lemma 3 (build). BUILD returns a node that is well formed for with .
Proof. By induction on . Line L1 handles the leaf. Otherwise lines L3 build well formed trees for the two halves with the right arrays, and line L4 makes a node whose sum is the sum of the two child sums.
Lemma 4 (update). If is well formed for and , then UPDATE returns a node that is well formed for , with equal to except that position holds . It makes exactly one new node per level, that is, nodes where is the depth of the leaf of .
Proof. By induction on . If then , and line L5 returns a leaf with sum . Otherwise let (the case is the same with the sides swapped). By induction, is well formed for with the array of changed at . The node is well formed for because is, and its array is the old one, which has no position . Line L9 makes a node with these children and with the sum of their sums, so it is well formed, and its array is the concatenation, which is the old array with position changed. One node is made at this level plus, by induction, one per lower level on the path.
Lemma 5 (query). If is well formed for , then QUERY returns the sum of over the positions in .
Proof. By induction on . If the ranges do not meet (line L10) the sum is empty, 0. If (line L11) the answer is the full sum, which is by Lemma 1. Otherwise , because a range of length 1 either is inside or does not meet it. Then is the disjoint union of its parts in and in , and line L13 adds the two answers, which are right by induction.
Theorem 1. After any sequence of operations, for each version the root is well formed for , and is the array that the abstract type gives to version . Each query returns the abstract answer on its version.
Proof. By induction on the number of operations. Version 0 is right by Lemma 3. An operation update(v, i, x) adds a root that is right by Lemma 4 applied to , which is well formed by induction. By Lemma 2 no earlier root changes its array. add is get followed by update. get follows the one path to the leaf of , and by the definition of that leaf holds position . query is right by Lemma 5 with .
Theorem 2 (kth). Let and be roots, both well formed for , with at each position, and let . Then KTH returns the least with .
Proof. Line K1 rejects other , using Lemma 1 for . Let be the answer. We claim that at the test of line K2 the nodes and are well formed for the current , , and equals for the original , with . This holds at the start. In a step, let be the value of line K3; by Lemma 1, . If , then the -th unit after position lies in , so , and line K4 keeps the claim with unchanged. If , then , and line K5 subtracts the units of , which keeps and . Each step halves the range, so the loop ends with , and then , which line K6 returns.