#C6572. Merge Overlapping Intervals

    ID: 50347 Type: Default 1000ms 256MiB

Merge Overlapping Intervals

Merge Overlapping Intervals

You are given a list of intervals, where each interval is represented as a pair of integers \( (s, e) \) indicating its start and end. Your task is to merge all overlapping intervals and return a list of the merged intervals sorted by their start time.

Example:

Input: 4
       1 3
       2 6
       8 10
       15 18
Output:
       1 6
       8 10
       15 18

Note: Two intervals \( (s_1, e_1) \) and \( (s_2, e_2) \) overlap if \( s_2 \leq e_1 \). When intervals overlap, they should be merged into a single interval \( (s_1, \max(e_1, e_2)) \).

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer \( T \) representing the number of intervals.
  • Each of the next \( T \) lines contains two space-separated integers representing the start and end of an interval.

It is guaranteed that \( T \ge 1 \).

outputFormat

Output the merged intervals to standard output (stdout). Each merged interval should be printed on a new line with the start and end values separated by a space. The intervals must be sorted by their starting time.

## sample
1
1 3
1 3

</p>