QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#13068. Intervals

Statistics

Let interval $[s, t]$ denote the set of integers between $s$ and $t$, inclusively. Given a set of $n$ intervals $A = \{[s_i, t_i]\}$, compute a set of intervals $B$ (not necessary a subset of $A$), such that each $[s_i, t_i] \in A$ can be presented as the union (set-union) of some intervals in $B$. The goal is to minimize the number of intervals in $B$.

Input

There are at most $100$ test cases. Each case will consist of one integer $n$, following $n$ lines each consists of $s_i$ and $t_i$, separated by a space.

$1 \leq n \leq 1000$, $0 \leq s_i \leq t_i \leq 10^9$.

Output

For each test case, print a single line containing one integer $m$, the number of intervals. The $j$-th line of the following $m$ lines contain the $j$-th interval as $s_j$ and $t_j$, the intervals can be in any order. If there are multiple solutions with the same number of intervals, any of them will be accepted.

Sample Input

2
1 2
3 4
3
1 2
3 4
1 4
7
5 9
0 6
4 8
3 7
0 5
7 9
4 6

Sample Output

2
1 2
3 4
2
1 2
3 4
5
0 5
4 6
3 7
5 8
7 9