Contributions: Codex (GPT-6) specified the stack rules and supplied the visibility segment tree, block alternative, proofs, implementations, and verification.
An element remains in the final stack exactly when no later element is at least as large. Such a later element eventually pops it; without one, no later insertion can pop it. Thus the answer is the number of strict record maxima when scanning from right to left.
Store each segment-tree node's maximum
For children
To evaluate
- If
$M_R\le t$ , every right element is blocked. Recurse only into$F(L,t)$ . - If
$M_R>t$ , the right maximum survives and fixes the left threshold at$M_R$ . The left contribution is already stored as$C_u-C_R$ . Recurse only into$F(R,t)$ and add that difference.
Only one child is visited per level, so
After a point update, recompute maxima and record counts toward the root. Each merge makes one
Process the canonical nodes of a query from right to left. Start the threshold at
The record equivalence, merge identity, and threshold invariant prove that each answer equals the specified stack size. Equality must block a left element, consistent with strict record maxima.
Let
std.cpp is the preferred implementation. The block alternative updates in
Each formal input has five timing runs and a separate peak-memory run. The standard program's slowest median is 0.055566 seconds and peak memory is 6.90 MiB. Twice the median, rounded up to 0.5 seconds, gives a 0.5-second limit. The alternative's slowest median is 0.063319 seconds and peak memory is 5.62 MiB. See the environment and individual measurements. Recalibrate in other environments.