#K62897. Merge Overlapping Segments

    ID: 31633 Type: Default 1000ms 256MiB

Merge Overlapping Segments

Merge Overlapping Segments

You are given n practice segments, where each segment is defined by its start and end times. Sometimes these segments overlap or even touch each other. Your task is to merge all overlapping or contiguous segments and output the resulting non-overlapping segments in sorted order.

Formally, given a set of intervals \( [a_i, b_i] \) for \( i = 1, 2, \dots, n \), merge all intervals that overlap or touch (i.e., if one segment's start time is less than or equal to the previous segment's end time), and output the set of merged segments in ascending order. Use LaTeX format for any formulas (see above for the mathematical notation).

For example, if the input segments are \( [1, 3], [2, 6], [8, 10] \), then merging them will yield \( [1, 6], [8, 10] \>.

inputFormat

The first line of input contains an integer n representing the number of segments. Each of the next n lines contains two integers separated by space, which represent the start time and end time of a segment.

outputFormat

Output the merged segments, one per line. For each merged segment, print two integers (the start and end times) separated by a space. The segments should be output in ascending order based on their start times.

## sample
3
1 3
2 6
8 10
1 6

8 10

</p>