#K11886. Merge Overlapping Intervals

    ID: 23568 Type: Default 1000ms 256MiB

Merge Overlapping Intervals

Merge Overlapping Intervals

You are given a collection of intervals, where each interval is represented by two integers \(a\) and \(b\) (denoting the start and end). Your task is to merge all overlapping or consecutive intervals and output the resulting collection of non-overlapping intervals in sorted order.

For instance, the intervals [1, 3] and [2, 6] overlap and should be merged into [1, 6]. If two intervals touch (e.g., [1, 4] and [4, 5]), they are also merged to form [1, 5].

You need to read the input from standard input (stdin) and write the output to standard output (stdout).

inputFormat

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

outputFormat

The first line of output should contain an integer \(m\) representing the number of merged intervals. Each of the next \(m\) lines should contain two space-separated integers, indicating the start and end of a merged interval.

## sample
0
0