#C7940. Merge Intervals

    ID: 51867 Type: Default 1000ms 256MiB

Merge Intervals

Merge Intervals

Given a collection of intervals, your task is to merge all overlapping or contiguous intervals into a list of non-overlapping intervals. Two intervals ( [a, b] ) and ( [c, d] ) are considered overlapping if ( b \ge c ). The merged intervals should be output in increasing order of their starting points.

For example, given the intervals [1, 3], [2, 4], and [5, 7], the merged result is [1, 4] and [5, 7].

inputFormat

The input is read from standard input (stdin).

The first line contains an integer ( N ), which represents the number of intervals. The following ( N ) lines each contain two space-separated integers indicating the start and end of an interval.

outputFormat

The output should be printed to standard output (stdout).

Print each merged interval on a separate line, with the two integers (start and end) separated by a single space. The intervals must be printed in increasing order of their starting points.## sample

3
1 3
2 4
5 7
1 4

5 7

</p>