CF1389D.Segment Intersections

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two lists of segments [al1,ar1],[al2,ar2],…,[aln,arn][al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n] and [bl1,br1],[bl2,br2],…,[bln,brn][bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n].

Initially, all segments [ali,ari][al_i, ar_i] are equal to [l1,r1][l_1, r_1] and all segments [bli,bri][bl_i, br_i] are equal to [l2,r2][l_2, r_2].

In one step, you can choose one segment (either from the first or from the second list) and extend it by 11. In other words, suppose you've chosen segment [x,y][x, y] then you can transform it either into [x−1,y][x - 1, y] or into [x,y+1][x, y + 1].

Let's define a total intersection II as the sum of lengths of intersections of the corresponding pairs of segments, i.e. \sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}. Empty intersection has length 00 and length of a segment [x,y][x, y] is equal to y−xy - x.

What is the minimum number of steps you need to make II greater or equal to kk?

给你两组线段:[al1,ar1],[al2,ar2],…,[aln,arn][al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n] 和 [bl1,br1],[bl2,br2],…,[bln,brn][bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]。

初始时,所有线段 [ali,ari][al_i, ar_i] 均等于 [l1,r1][l_1, r_1],所有线段 [bli,bri][bl_i, br_i] 均等于 [l2,r2][l_2, r_2]。

每一步中,你可以选择其中一组(第一组或第二组)中的任意一个线段,并将其长度扩展 11。换言之,若你选择了线段 [x,y][x, y],则可将其变为 [x−1,y][x - 1, y] 或 [x,y+1][x, y + 1]。

定义总交集长度 II 为所有对应线段对交集长度之和,即

\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}.

空交集的长度为 00;线段 [x,y][x, y] 的长度定义为 y−xy - x。

问:使 I≥kI \ge k 所需的最少操作步数是多少?

输入格式

The first line contains the single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 1≤k≤1091 \le k \le 10^9) — the length of lists and the minimum required total intersection.

The second line of each test case contains two integers l1l_1 and r1r_1 (1≤l1≤r1≤1091 \le l_1 \le r_1 \le 10^9) — the segment all [ali,ari][al_i, ar_i] are equal to initially.

The third line of each test case contains two integers l2l_2 and r2r_2 (1≤l2≤r2≤1091 \le l_2 \le r_2 \le 10^9) — the segment all [bli,bri][bl_i, br_i] are equal to initially.

It's guaranteed that the sum of nn doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;1≤k≤1091 \le k \le 10^9)—— 列表的长度以及所需的最小总交集大小。

每个测试用例的第二行包含两个整数 l1l_1 和 r1r_1(1≤l1≤r1≤1091 \le l_1 \le r_1 \le 10^9)—— 所有区间 [ali,ari][al_i, ar_i] 的初始公共区间。

每个测试用例的第三行包含两个整数 l2l_2 和 r2r_2(1≤l2≤r2≤1091 \le l_2 \le r_2 \le 10^9)—— 所有区间 [bli,bri][bl_i, br_i] 的初始公共区间。

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

输出格式

Print tt integers — one per test case. For each test case, print the minimum number of step you need to make II greater or equal to kk.

输出 tt 个整数——每个测试用例输出一个整数。对于每个测试用例,输出使 II 大于或等于 kk 所需的最少步数。

输入输出样例

  • 输入#1

    3
    3 5
    1 2
    3 4
    2 1000000000
    1 1
    999999999 999999999
    10 3
    5 10
    7 8

    输出#1

    7
    2000000000
    0

说明/提示

In the first test case, we can achieve total intersection 55, for example, using next strategy:

  • make [al1,ar1][al_1, ar_1] from [1,2][1, 2] to [1,4][1, 4] in 22 steps;
  • make [al2,ar2][al_2, ar_2] from [1,2][1, 2] to [1,3][1, 3] in 11 step;
  • make [bl1,br1][bl_1, br_1] from [3,4][3, 4] to [1,4][1, 4] in 22 steps;
  • make [bl2,br2][bl_2, br_2] from [3,4][3, 4] to [1,4][1, 4] in 22 steps.

In result, I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5

In the second test case, we can make [al1,ar1]=[0,1000000000][al_1, ar_1] = [0, 1000000000] in 10000000001000000000 steps and [bl1,br1]=[0,1000000000][bl_1, br_1] = [0, 1000000000] in 10000000001000000000 steps.

In the third test case, the total intersection II is already equal to 10>310 \gt 3, so we don't need to do any steps.

在第一个测试用例中,我们可以通过以下策略实现总交集长度为 55:

  • 将 [al1,ar1][al_1, ar_1] 从 [1,2][1, 2] 扩展为 [1,4][1, 4],耗时 22 步;
  • 将 [al2,ar2][al_2, ar_2] 从 [1,2][1, 2] 扩展为 [1,3][1, 3],耗时 11 步;
  • 将 [bl1,br1][bl_1, br_1] 从 [3,4][3, 4] 扩展为 [1,4][1, 4],耗时 22 步;
  • 将 [bl2,br2][bl_2, br_2] 从 [3,4][3, 4] 扩展为 [1,4][1, 4],耗时 22 步。

最终,

I=intersection_length([al1,ar1],[bl1,br1])+intersection_length([al2,ar2],[bl2,br2])++intersection_length([al3,ar3],[bl3,br3])=3+2+0=5I = \text{intersection\_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection\_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection\_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5

在第二个测试用例中,我们可以在 10000000001000000000 步内将 [al1,ar1][al_1, ar_1] 变为 [0,1000000000][0, 1000000000],并在 10000000001000000000 步内将 [bl1,br1][bl_1, br_1] 变为 [0,1000000000][0, 1000000000]。

在第三个测试用例中,总交集长度 II 已为 10>310 > 3,因此无需执行任何操作。

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

首页