#C1281. Merge Overlapping Intervals

    ID: 42278 Type: Default 1000ms 256MiB

Merge Overlapping Intervals

Merge Overlapping Intervals

Given a list of intervals of the form [start, end], your task is to merge all overlapping intervals and output a list of non-overlapping intervals sorted by their starting values.

Two intervals \([a, b]\) and \([c, d]\) are considered overlapping if \(b \ge c\). When intervals overlap, they should be merged into a single interval \([\min(a,c), \max(b,d)]\).

Implement a solution that reads the intervals from the standard input and writes the merged intervals to the standard output.

inputFormat

The input is given from standard input in the following format:

 n
 s1 e1
 s2 e2
 ...
 sn en

Here, n is the number of intervals, and each of the next n lines contains two space-separated integers that represent the start (s) and end (e) of an interval.

outputFormat

Output the merged intervals to the standard output. Print each merged interval on a separate line as two integers separated by a space. The intervals must be output in increasing order by their start values.

## sample
6
1 3
2 6
8 10
15 18
17 20
20 25
1 6

8 10 15 25

</p>