luogu#P9674. [ICPC2022 Jinan R] Set of Intervals

[ICPC2022 Jinan R] Set of Intervals

题目描述

Prof. Pang has a multi-set of intervals S={[li,ri]}S=\{[l_i,r_i]\}(li<ril_i<r_i).

Prof. Pang will perform the following operation for S1|S|-1 times:

  • Select two intervals [a,b][a,b] and [c,d][c,d] from SS, and then choose two integers x,yx,y satisfying x[a,b],y[c,d],x<yx\in [a,b], y\in [c,d], x<y. After that, delete [a,b][a,b] and [c,d][c,d] from SS, and add [x,y][x,y] to SS.

It's easy to find that SS contains exactly one interval after the operations, and Prof. Pang will get the interval as a gift.

Now Prof. Pang wants you to calculate how many different intervals he can get.

输入格式

The first line contains one integer T T~(1T1041\le T \le 10^4), the number of test cases.

For each test case, the first line contains one integer n n~(1n1051\le n\le 10^5) --- the size of SS. Each of the following nn lines contains two integers lil_i and ri r_i~(1li<ri1091\le l_i<r_i\le 10^9), describing the ii-th interval in SS.

It is guaranteed that the sum of nn over all test cases is no more than 10510^5.

输出格式

For each test case, output one line containing the answer to Prof. Pang's question.

题目大意

蓝蓝有一个元素全为闭区间的可重集 S={[li,ri]}S=\{[l_i,r_i]\}(li<ril_i<r_i)。

蓝蓝将会执行以下操作 n1n-1 次:

  • SS 内选择两个闭区间 [a,b][a,b][c,d][c,d],再选择两个整数 x,yx,y 满足 x[a,b],y[c,d],x<yx\in [a,b], y\in [c,d], x<y,然后从 SS 中删去 [a,b][a,b][c,d][c,d],并把 [x,y][x,y] 添加进 SS

显然最终 SS 中有且仅有一个区间。现在蓝蓝想知道她可以获得多少不同的区间。

4
1
1 1000000000
2
1 1000000000
1 1000000000
4
1 2
3 4
5 6
7 8
4
1 3
2 4
5 8
6 7
1
499999999500000000
26
28