Segment Tree Beats: The Strict Second Maximum Is a Certificate
Ordinary lazy propagation works when a node summary can always absorb an update. Range breaks that assumption: replacing every by changes the sum by an amount that depends on the value distribution.
Segment tree beats does not force a universal lazy tag. It recognizes when the available summary is sufficient, and descends only when the summary cannot certify a safe update.
What the node must know
Store the sum, largest value , largest value strictly below it , and number of occurrences of . Use a negative-infinity sentinel for when the segment is constant.
For a fully covered node, three cases are possible:
| Condition | Action |
|---|---|
| Nothing changes | |
| Only maximum elements change | |
| Descend into children |
In the middle case, subtract from the sum and replace with . The count stays the same, and stays strictly smaller. That is the certificate: the maximum group is isolated from every other value.
The equality trap
Take the segment . Its summary is sum 27, maximum 9, strict second maximum 6, count 2. Capping at 7 reduces the sum by four, so the new sum is 23.
Capping at 6 is different. The two nines join the existing six, making the maximum count three. The old summary does not say how many sixes existed, so changing only the maximum and sum cannot maintain a valid summary. Descend when . Replacing the strict inequality with a non-strict one is not a harmless optimization.
Merge and push contracts
To merge children, choose the larger maximum. If both maxima are equal, add their counts and take the larger strict second maximum. Otherwise the losing child’s maximum competes with the winning child’s second maximum for the new second maximum.
A parent cap can be pushed to a child only if the child’s maximum exceeds the parent’s maximum. The beats invariant guarantees that this cap lies strictly above that child’s second maximum. Assert this property while developing the implementation; it catches subtle merge bugs close to their origin.
apply_cap(node, x): require node.second < x < node.maximum node.sum -= (node.maximum - x) * node.maximum_count node.maximum = x
push(parent): for child in children(parent): if child.maximum > parent.maximum: apply_cap(child, parent.maximum)The pseudocode intentionally separates applicability from application. Arbitrary calls to apply_cap are not safe.
Why the extra descent is affordable
For the restricted operation set of range chmin and sum/max queries, consider the potential equal to the sum, over tree nodes, of the number of distinct values in each segment. Initially it is .
A fully covered node that must descend has at least two distinct values at or above the cap. The operation merges those levels and decreases its distinct-value count. Nodes partially intersected by the query can introduce at most one new value each; there are only such boundary nodes per update. Thus expensive full-cover descents are paid for by initial potential and boundary increases. The total work is for this restricted variant, rather than a worst-case logarithmic bound on every individual update.
Adding range increments, chmax, historical extrema, or assignment changes the state and the complexity argument. Do not reuse this proof without checking the added operations. Common richer variants have an additional logarithmic factor in their amortized analysis.
Practice deliberately
Exercise. Implement only chmin and sum first. Compare against a plain vector on alternating high/low values, constant segments, negative caps, caps equal to the second maximum, and partial ranges that cut across the root midpoint. Then add range maximum queries. Keep the tiny brute-force oracle after optimizing.
Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline