Problem setter’s solution: split by minimum gap, with sliding windows and an eight-way bit tree. Codex (GPT-6) provides the proof organization and C++17 implementation, including explicit singleton and zero-gap handling. The core algorithm is preserved.
Let
For every left endpoint, extend the interval to the right while maintaining its minimum gap. Stop as soon as the gap becomes at most
When inserting
For each right endpoint
For
Before inserting the new value, remove elements from the left while its distance to the current predecessor or successor is less than
We need a gap of exactly
- If the inequality is strict, the window starting at
$L_d(r)$ satisfies bound$d$ but fails bound$d+1$ . All values are integers, so its minimum gap is exactly$d$ . Update the answer with its length. - If the starts are equal, the longest window already satisfies bound
$d+1$ , as does every suffix. No interval ending at$r$ has minimum gap exactly$d$ .
Reuse one array, replacing
For gap zero, the answer is the whole sequence length if any value repeats, and zero otherwise.
Represent the integer universe by a complete eight-way tree. Each internal node has an eight-bit mask identifying its nonempty children; a separate array records present values. Insertions and deletions update masks along one path. A predecessor or successor search climbs to a suitable neighboring nonempty branch and then descends through its last or first nonempty children. Precompute the first and last set bit for every eight-bit mask.
Here
The short-interval enumeration covers every gap greater than the threshold. The strict-start-change criterion covers every positive small gap, and the repeated-value check handles zero. Every recorded candidate corresponds to a real valid interval, so the maximum lengths are correct.
Time is
std.cpp uses the eight-way bit tree. A balanced tree has the same asymptotic bound, but allocations and pointer accesses affect constants and should be measured on the target judge.
Compile std.cpp , then run python tests/generate.py --out-dir <directory> --std <program> . The plan covers singletons, duplicates, alternating values, spaced values, permutations, and random sequences. The independent small-instance checker enumerates every pair inside every interval. See environment and measurements for calibration.