Intervals
InterviewTime limit1sMemory limit128 MB
Given n closed intervals, merge all overlapping or touching ones and print the resulting disjoint intervals in ascending order.
Problem
You are given closed intervals , where . The union of these intervals can be represented as a sum of pairwise non-intersecting closed intervals. Find such a representation that uses the minimal number of intervals, and print those intervals in ascending order. Two intervals and are in ascending order if and only if .
Write a program that:
- reads the description of the intervals from standard input,
- computes the pairwise non-intersecting intervals satisfying the conditions above,
- writes the computed intervals in ascending order to standard output.
Input
The first line contains one integer , the number of intervals (). Each of the next lines contains a description of interval as two integers and separated by a single space, the beginning and the end of the interval respectively, with .
Output
Print the descriptions of all computed pairwise non-intersecting intervals. Each line contains the description of one interval: two integers separated by a single space, the beginning and the end of the interval respectively. The intervals must be printed in ascending order.