A satellite's onboard log records the intervals of its orbit during which its solar panel is in direct sunlight, given as a list of [start, end] pairs sorted by start time, where no two intervals overlap (though two may touch end-to-end). Mission control has identified an eclipse interval [rs, re] during which the satellite passes into a planet's shadow and receives no sunlight, regardless of what the log says.
For every sunlight interval, remove whatever portion of it falls inside the eclipse interval: split the interval into two pieces if the eclipse cuts through its middle, shrink it if the eclipse only clips one end, leave it untouched if the eclipse doesn't reach it, or discard it entirely if the eclipse fully covers it. Report the resulting list of sunlight intervals, in order of start time, keeping only intervals with strictly positive length.
Line 1: an integer n — the number of sunlight intervals.
Line 2: two integers rs and re — the eclipse interval.
Each of the next n lines contains two integers s and e — one sunlight interval, in order of increasing s.
First line: an integer m — the number of resulting sunlight intervals.
Each of the next m lines: two integers s and e, the resulting interval, in order of increasing s.
s and pairwise non-overlapping (the next interval's s is >= the previous interval's e).Example 1
Input
3 1 6 0 2 3 4 5 7
Expected
2 0 1 6 7
Explanation
The eclipse is [1,6]. Interval [0,2] straddles the left eclipse boundary, keeping only its part before rs=1, giving [0,1]. Interval [3,4] lies entirely inside [1,6] and is dropped. Interval [5,7] straddles the right eclipse boundary, keeping only its part after re=6, giving [6,7]. The result is [0,1] and [6,7], so output is 2 followed by those two lines.
Example 2
Input
1 0 5 0 5
Expected
0
Explanation
The single sunlight interval [0,5] exactly matches the eclipse interval [0,5], so it is entirely covered and removed, leaving no intervals. Output is just 0 with no further lines.
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 →