#abc256d. D - Union of Interval

D - Union of Interval

Score : 400400 points

问题描述

对于实数 LLRR,我们用 [L,R)[L,R) 表示所有大于等于 LL 且小于 RR 的实数集合。这样的集合被称为右半开区间。

现在给你 NN 个右半开区间 [Li,Ri)[L_i,R_i)。设它们的并集为 SS。请将集合 SS 表示为尽可能少的右半开区间的并集。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

For real numbers LL and RR, let us denote by [L,R)[L,R) the set of real numbers greater than or equal to LL and less than RR. Such a set is called a right half-open interval.

You are given NN right half-open intervals [Li,Ri)[L_i,R_i). Let SS be their union. Represent SS as a union of the minimum number of right half-open intervals.

Constraints

  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1Li<Ri2×1051 \leq L_i \lt R_i \leq 2\times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

L1L_1 R1R_1

\vdots

LNL_N RNR_N

Output

Let kk be the minimum number of right half-open intervals needed to represent SS as their union. Print kk lines containing such kk right half-open intervals [Xi,Yi)[X_i,Y_i) in ascending order of XiX_i, as follows:

X1X_1 Y1Y_1

\vdots

XkX_k YkY_k

Sample Input 1

3
10 20
20 30
40 50

Sample Output 1

10 30
40 50

The union of the three right half-open intervals [10,20),[20,30),[40,50)[10,20),[20,30),[40,50) equals the union of two right half-open intervals [10,30),[40,50)[10,30),[40,50).

Sample Input 2

3
10 40
30 60
20 50

Sample Output 2

10 60

The union of the three right half-open intervals [10,40),[30,60),[20,50)[10,40),[30,60),[20,50) equals the union of one right half-open interval [10,60)[10,60).

update @ 2024/3/10 10:53:39