PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000256. Hợp các khoảng (Union of Interval)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hợp các khoảng (Union of Interval)

Union of Interval

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given NN half-open real intervals [Li,Ri)[L_i,R_i), each consisting of real xx with Li≤x<RiL_i\le x<R_i, express their union using the fewest half-open intervals. Adjacent intervals may be merged.

Input

First line: NN. The next NN lines contain Li,RiL_i,R_i.

Output

Print each resulting pair Xi,YiX_i,Y_i on its own line in ascending order of XiX_i. Do not print the number of intervals.

Subtasks

  • Subtask 1 (20 points): 1≤N≤1001\le N\le 100; 1≤Li<Ri≤1001\le L_i<R_i\le 100.
  • Subtask 2 (30 points): 1≤N≤2000001\le N\le 200000; 1≤Li<Ri≤2000001\le L_i<R_i\le 200000; Li=L1L_i=L_1.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 1≤Li<Ri≤2000001\le L_i<R_i\le 200000.

Example 1

Input

3
10 20
20 30
40 50

Output

10 30
40 50

Explanation

The first two are adjacent and merge into [10,30); the last is separate.

Example 2

Input

3
10 40
30 60
20 50

Output

10 60

Explanation

All three overlap into [10,60).