Problem 562
Remove Covered Intervals
Medium · 20 pts · Array, Sorting, Intervals
Problem
You are given N distinct intervals [a, b). An interval [a, b) is covered by another interval [c, d) if c <= a and b <= d. Remove every interval that is covered by some other interval in the list, and print how many intervals remain. Sort the intervals by start ascending and, for equal starts, by end descending; then an interval is covered exactly when its end is at most the largest end seen before it.
Input
The first line contains N. Each of the next N lines contains two integers a and b, one interval. All intervals are distinct.
Output
Print the number of intervals that remain.
Constraints
1 <= N <= 100000; 0 <= a < b <= 100000
Example input
3 1 4 3 6 2 8
Example output
2
Explanation
With intervals [1,4), [3,6) and [2,8), the interval [3,6) lies inside [2,8), so it is removed. The other two are not covered by anything, so 2 intervals remain.