Proofs: Li Chao tree
These are the human proofs of Li Chao tree, at full length.
The Li Chao tree keeps a lowest line on every root path full
This proof shows: correctness, complexity.
The domain is the index range with ; a line is a function of the index, and for two lines the difference is a linear function of , so it is monotone in . For a node over write for the path from the root to the leaf of .
Invariant (LC). (a) If a node holds no line, none of its descendants holds one. (b) For every inserted line and every index , some line stored on has .
Lemma 1 (one step). Let and be the node line and the carried line at a node over after line L5, so .
- If the walk stops at line L9, then for all .
- If it goes left at line L7, then for all .
- If it goes right at line L8, then for all .
Proof. Let , monotone in . (1) and , so between them. (2) , so is non-decreasing, and for . (3) and , so on .
Lemma 2 (INSERT keeps (LC)). After INSERT, (LC) holds for the old lines and .
Proof. (a) INSERT places a line only at the first empty node of its path, whose parent holds a line, and it never empties a node. For (b), consider the walk, with the carried line at the -th node . The lines that are "in flight" are those stored in the nodes and the carried line. We show the claim: after each step, for each line that is stored or carried at the start of the step and each index , either some stored line on is at , or lies in the range of the next node and the carried line is at .
At a swap on line L5 the two lines change places, so both are still stored or carried, at the same node. Then by Lemma 1, at the indices of that the walk leaves behind, the stored line is at most the carried line, and lies on for all of them. Indices outside are not affected: the carried line was carried into this node only for indices of its range. When the walk ends at line L3, the carried line is stored at a node whose range contains all its remaining indices. At line L6 the range has one index, which is , where after line L5. Lines stored below the path are not moved. By induction over the steps, every line, old or new, is covered on every index.
Theorem. QUERY returns over all inserted lines, or if there are none.
Proof. QUERY reads the stored lines on from the root down; by (a) it can stop at the first empty node, since none below holds a line. Every value it takes is the value of an inserted line, so the result is at least the minimum. By (b), for the line that attains the minimum at , some stored line on is at most , so the result is at most the minimum.
Cost. The tree over has at most levels. INSERT and QUERY visit one node per level and evaluate at most four lines per node, so both take time. Each INSERT fills at most one empty node, so at most nodes hold lines.