An airport runway is monitored over the time horizon from minute 0 to minute T. There are n maintenance windows, each a closed range [start, end] guaranteed to lie within [0, T]. A minute of the horizon is idle if it is not inside any maintenance window. Report the total length of the idle part of [0, T] (the length of [0, T] not covered by any window).
Line 1: two integers n and T.
Next n lines: two integers start and end describing one window (0 ≤ start ≤ end ≤ T).
A single integer: the total idle length within [0, T].
Example 1
Input
3 10 0 2 5 7 9 10
Expected
5
Explanation
The windows cover 2 + 2 + 1 = 5 minutes of [0,10], leaving 10 - 5 = 5 idle minutes (the gaps [2,5] and [7,9]).
Example 2
Input
1 8 0 8
Expected
0
Explanation
A single window covers the entire horizon [0,8], so there are no idle minutes.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →