#P16348. 「MierOI R1」Eternal (Hard ver.)

    ID: 18427 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>动态规划 DP线段树二分根号分治

「MierOI R1」Eternal (Hard ver.)

Background

This problem is a harder version of P16280. The only difference between the two versions is that in this problem, n5×104n \le 5 \times 10^4.

Problem Description

Given nn closed intervals [l1,r1],[l2,r2],,[ln,rn][l_1,r_1],[l_2,r_2],\dots,[l_n,r_n]. Find the maximum number of intervals that can be selected such that for any two selected intervals that intersect^{\bm{\dagger}}, they share a common endpoint^{\bm{\ddagger}}.


\bm\dagger Two closed intervals [l1,r1][l_1,r_1] and [l2,r2][l_2,r_2] are said to intersect if and only if l2r1l_2 \le r_1 and l1r2l_1 \le r_2.
\bm\ddagger Two closed intervals [l1,r1][l_1,r_1] and [l2,r2][l_2,r_2] are said to share a common endpoint if and only if l1=l2l_1=l_2, l1=r2l_1=r_2, r1=l2r_1=l_2, or r1=r2r_1=r_2.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, representing the number of test cases.

Then follow TT test cases. For each test case:

  • The first line contains a positive integer nn.
  • The next nn lines each contain two positive integers li,ril_i,r_i.

Output Format

For each test case, output one line containing one integer, representing the maximum number of intervals that can be selected.

4
3
1 3
2 2
1 1
5
1 3
2 3
4 5
3 5
1 4
8
1 4
2 4
3 4
1 2
2 3
1 3
3 5
4 5
16
1 4
2 4
3 4
1 2
2 3
1 3
3 5
4 5
5 8
6 8
7 8
5 6
6 7
5 7
7 9
8 9
2
4
6
12

Hint

"Sample #1 Explanation"

For the first test case, you can select two intervals, [1,1][1,1] and [1,3][1,3]. It can be proven that there is no way to select more intervals.

For the second test case, you can select four intervals, [1,3][1,3], [2,3][2,3], [4,5][4,5], and [3,5][3,5]. It can be proven that there is no way to select more intervals.

Constraints

This problem has no subtasks.

For all testdata, it is guaranteed that 1T51 \le T \le 5, 1n5×1041 \le n \le 5 \times 10^4, 1liri2n1 \le l_i \le r_i \le 2n.

Translated by ChatGPT 5