Editorial
Let be the maximum difficulty before problem , and the minimum difficulty after it. Use when there is no earlier problem and when there is no later problem. The number of victims whose first failure is problem is
Sum this expression over all . Precompute the suffix minima , then scan from left to right while maintaining . Both time and space complexity are .
Proof
With the problems sorted by difficulty, a participant of skill solves every problem of difficulty at most . They are therefore a victim exactly when a solvable problem appears after the first problem that stops them.
Problem is the first failure exactly when , and a later problem is solvable exactly when . The number of skill values satisfying both conditions is . Each victim has a unique first failure, so the sum counts every victim exactly once.