A city reports n road closures along a single street, each given as a range [start_i, end_i] measured in meters from the start of the street. Closures may overlap or touch each other. Print the total length (in meters) of street that is covered by at least one closure, i.e. the length of the union of all the given ranges.
Line 1: an integer n.
Next n lines: two integers start_i end_i describing closure i.
A single integer: the total length of street covered by at least one closure.
Example 1
Input
3 0 5 3 8 10 12
Expected
10
Explanation
Closures [0,5] and [3,8] merge into [0,8] (length 8); [10,12] is separate (length 2). Total = 10.
Example 2
Input
2 1 4 4 7
Expected
6
Explanation
The two closures touch at 4 and merge into [1,7], a single stretch of length 6.
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 →