A highway is marked with integer mileposts. There are n inspection ranges, each a closed range [start, end] covering every integer milepost x with start <= x <= end (a range with start == end covers exactly one milepost). Count the number of distinct integer mileposts covered by at least one range. Note that two ranges like [1,3] and [4,6] cover disjoint integer mileposts even though no gap exists between the integers 3 and 4.
Line 1: an integer n.
Next n lines: two integers start and end describing one range.
A single integer: the count of distinct covered integer mileposts.
Example 1
Input
2 1 4 3 6
Expected
6
Explanation
Together the ranges cover mileposts 1,2,3,4,5,6 -> 6 distinct mileposts.
Example 2
Input
2 1 3 5 7
Expected
6
Explanation
The ranges cover {1,2,3} and {5,7,6} = {5,6,7}, giving 6 distinct mileposts.
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 →