CF1884C.Medium Design

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The array a1,a2,…,ama_1, a_2, \ldots, a_m is initially filled with zeroes. You are given nn pairwise distinct segments 1≤li≤ri≤m1 \le l_i \le r_i \le m. You have to select an arbitrary subset of these segments (in particular, you may select an empty set). Next, you do the following:

  • For each i=1,2,…,ni = 1, 2, \ldots, n, if the segment (li,ri)(l_i, r_i) has been selected to the subset, then for each index li≤j≤ril_i \le j \le r_i you increase aja_j by 11 (i. e. aja_j is replaced by aj+1a_j + 1). If the segment (li,ri)(l_i, r_i) has not been selected, the array does not change.
  • Next (after processing all values of i=1,2,…,ni = 1, 2, \ldots, n), you compute max⁡(a)\max(a) as the maximum value among all elements of aa. Analogously, compute min⁡(a)\min(a) as the minimum value.
  • Finally, the cost of the selected subset of segments is declared as max⁡(a)−min⁡(a)\max(a) - \min(a).

Please, find the maximum cost among all subsets of segments.

数组 a1,a2,…,ama_1, a_2, \ldots, a_m 初始时全部为零。给定 nn 个两两互不相交的区间 1≤li≤ri≤m1 \le l_i \le r_i \le m。你需要从中任选一个子集(特别地,该子集可以为空)。接着,执行以下操作:

  • 对每个 i=1,2,…,ni = 1, 2, \ldots, n,若区间 (li,ri)(l_i, r_i) 被选入该子集,则对每个下标 li≤j≤ril_i \le j \le r_i,将 aja_j 的值加 11(即 aja_j 被替换为 aj+1a_j + 1);若区间 (li,ri)(l_i, r_i) 未被选中,则数组保持不变。
  • 接着(在处理完所有 i=1,2,…,ni = 1, 2, \ldots, n 后),计算 max⁡(a)\max(a),即数组 aa 中所有元素的最大值;类似地,计算 min⁡(a)\min(a),即数组 aa 中所有元素的最小值。
  • 最后,定义所选区间子集的代价为 max⁡(a)−min⁡(a)\max(a) - \min(a)。

请找出所有区间子集中所能达到的最大代价。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤1051 \le n \le 10^5, 1≤m≤1091 \le m \le 10^9) — the number of segments and the length of the array.

The following nn lines of each test case describe the segments. The ii-th of these lines contains two integers lil_i and rir_i (1≤li≤ri≤m1 \le l_i \le r_i \le m). It is guaranteed that the segments are pairwise distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤1051 \le n \le 10^5,1≤m≤1091 \le m \le 10^9)——分别表示线段的数量和数组的长度。

每个测试用例接下来的 nn 行用于描述这些线段。其中第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤m1 \le l_i \le r_i \le m)。保证所有线段两两互不相同。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output the maximum cost among all subsets of the given set of segments.

对于每个测试用例,输出给定线段集合的所有子集中最大的代价。

输入输出样例

  • 输入#1

    6
    1 3
    2 2
    3 8
    2 4
    3 5
    4 6
    6 3
    1 1
    1 2
    1 3
    2 2
    2 3
    3 3
    7 6
    2 2
    1 6
    1 2
    5 6
    1 5
    4 4
    3 6
    6 27
    6 26
    5 17
    2 3
    20 21
    1 22
    12 24
    4 1000000000
    2 999999999
    3 1000000000
    123456789 987654321
    9274 123456789

    输出#1

    1
    3
    2
    3
    4
    4

说明/提示

In the first test case, there is only one segment available. If we do not select it, then the array will be a=[0,0,0]a = [0, 0, 0], and the cost of such (empty) subset of segments will be 00. If, however, we select the only segment, the array will be a=[0,1,0]a = [0, 1, 0], and the cost will be 1−0=11 - 0 = 1.

In the second test case, we can select all the segments: the array will be a=[0,1,2,3,2,1,0,0]a = [0, 1, 2, 3, 2, 1, 0, 0] in this case. The cost will be 3−0=33 - 0 = 3.

在第一个测试用例中,仅有一个可用的区间。如果我们不选择它,则数组为 a=[0,0,0]a = [0, 0, 0],该(空)区间子集的代价为 00。然而,如果我们选择这唯一的一个区间,则数组为 a=[0,1,0]a = [0, 1, 0],代价为 1−0=11 - 0 = 1。

在第二个测试用例中,我们可以选择所有区间:此时数组为 a=[0,1,2,3,2,1,0,0]a = [0, 1, 2, 3, 2, 1, 0, 0]。代价为 3−0=33 - 0 = 3。

输入解题思路,AI测评打分。不知道怎么写?

首页