#C13439. Merging Overlapping Intervals

    ID: 42977 Type: Default 1000ms 256MiB

Merging Overlapping Intervals

Merging Overlapping Intervals

You are given a list of intervals. Your task is to merge all overlapping intervals and output a sorted list of disjoint intervals. Two intervals \(I_i = [a_i, b_i]\) and \(I_j = [a_j, b_j]\) are considered overlapping if \(a_j \le b_i\). For example, merging the intervals \([1, 3]\) and \([2, 5]\) results in \([1, 5]\).

The final merged intervals should be output in increasing order by their start times.

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains a single integer \(n\) that indicates the number of intervals.
  • The next \(n\) lines each contain two space-separated integers representing the start and end of an interval.

It is guaranteed that \(n \ge 0\). In case \(n = 0\), no interval is provided and no output should be produced.

outputFormat

Output the merged intervals to stdout as follows:

  • Each merged interval is printed on a separate line.
  • Each line contains two space-separated integers representing the start and end of the merged interval.
## sample
3
1 3
5 7
8 10
1 3

5 7 8 10

</p>