Proofs: Two-dimensional segment tree
These are the human proofs of Two-dimensional segment tree, at full length.
The two-dimensional segment tree keeps the rectangle products and returns rectangle queries full
This proof shows: correctness, termination.
Let be a commutative monoid. For a finite set of cells of write , which is well defined by associativity and commutativity, with , and for disjoint . For an outer node with rows and an inner node with columns , the invariant is
We use the facts about the shape of a one-dimensional segment tree from the segment tree page: the children of a node with range , , have the ranges and , which split it; a node with is a leaf; and the recursion depth is at most .
Termination. Every recursive call of BUILD-X, BUILD-Y, QUERY-X, QUERY-Y, UPDATE-X and UPDATE-Y is on a child range, which is strictly shorter than the range of the caller, and no call recurses on a range of length 1. So each call tree is finite.
Lemma 1 (build). After BUILD-X, (T) holds for every outer node and every inner node .
Proof. By induction on the outer recursion: assume that (T) holds for all cells of the outer children of when line L7 runs, which line L6 ensures by the hypothesis, since it builds them first; outer leaves have no children. We show that BUILD-Y on makes (T) true for each , by induction on the inner recursion. At an inner leaf, . If is an outer leaf, and line L2 stores . Otherwise, is the disjoint union of and , and by the outer hypothesis line L2 stores . At an inner node that is not a leaf, line L4 builds both inner children first, so by the inner hypothesis line L5 stores , since and split .
Lemma 2 (inner query). If (T) holds for , then QUERY-Y, with , returns .
Proof. By induction on . If the two ranges are disjoint, line L8 returns , the product of the empty set. If , line L9 returns , which is right by (T). Otherwise , since a range of length 1 that meets lies inside it. Then is the disjoint union of its intersections with and , and line L10 returns the product of the two results, which are right by the hypothesis.
Lemma 3 (outer query). If (T) holds everywhere, then QUERY-X, with , returns for .
Proof. By induction on , as in Lemma 2. Line L11 returns for disjoint rows. Line L12 returns, by Lemma 2 at the inner root with , the value , which is right when . Otherwise the rows split between the two children, and line L13 combines the two correct parts, which are products over disjoint sets of cells.
Lemma 4 (update). Let (T) hold for a matrix , let differ from only at , and let the stored matrix be when UPDATE-X is called. After the call, (T) holds for .
Proof. A cell has a different right side in (T) for and for only if and . The outer nodes with are the nodes on the root-to-leaf path of row , and the inner nodes with are the nodes on the path of column . So only the cells of need a new value.
The recursion of UPDATE-X visits exactly the nodes of , and for each of them it calls UPDATE-Y only after the call on its child in has returned (line L19 before line L20). We show by induction from the outer leaf upward that, when UPDATE-Y on returns, all cells satisfy (T) for . Cells with did not change and were right. UPDATE-Y visits the nodes of from the root down to the leaf and recomputes them on the way back. At the inner leaf, line L15 reads if is an outer leaf, or the cells and otherwise: one child is in and right for by the outer hypothesis, and the other child's rows do not hold , so its cells were right and unchanged. As in Lemma 1, the new value is right. Each inner node of above the leaf is recomputed at line L18 from its two inner children, of which the one in was just made right and the other one did not change. So all cells of are right for . At the outer root this gives (T) everywhere.
Theorem. After BUILD-X on and any sequence of updates, each done by setting one entry and calling UPDATE-X, a call QUERY-X with and returns for the current matrix.
Proof. By Lemma 1 and Lemma 4, (T) holds for the current matrix. By Lemma 3 at the outer root, with , the query returns . All calls end by the termination argument.