Platforms at the Junction
Riverbend Junction has n trains passing through today. Train i pulls in at minute a_i and
pulls out at minute d_i, occupying a platform for every minute of the closed range [a_i, d_i]
(both the arrival minute and the departure minute included).
Two trains conflict if the ranges [a_i, d_i] and [a_j, d_j] share at least one minute. In
particular, if one train departs at exactly the minute another arrives, they still conflict and
cannot be given the same platform — a platform is occupied through the whole of its departure
minute, so there is no instant left free for a new train to pull in.
The station master wants to assign every train a platform so that no two conflicting trains ever share one. Help her find the minimum number of platforms that makes this possible.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·10^5), the number of trains.
Each of the next n lines contains two integers a_i and d_i (1 ≤ a_i ≤ d_i ≤ 10^9), the arrival
and departure minute of the i-th train.
Output
Print a single integer: the minimum number of platforms needed so that every train gets one and no two conflicting trains share a platform.
Notes
In the first example, trains 1 and 2 both occupy a platform during minute 10 (train 1's range is
[10, 10] and train 2's range is [10, 12]), so they need separate platforms. Train 2 and train 3
overlap during minutes 11 and 12. Train 1 and train 3 never overlap. At minute 10 two platforms are
in use, and at minutes 11–12 two platforms are in use again (train 2 and train 3), so 2 platforms
suffice and are necessary.
In the second example the two trains' ranges [1, 3] and [5, 7] never touch, so a single platform
can serve both, one after the other.