CF1389D.Segment Intersections
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two lists of segments [al1,ar1],[al2,ar2],…,[aln,arn] and [bl1,br1],[bl2,br2],…,[bln,brn] .
Initially, all segments [ali,ari] are equal to [l1,r1] and all segments [bli,bri] are equal to [l2,r2] .
In one step, you can choose one segment (either from the first or from the second list) and extend it by 1 . In other words, suppose you've chosen segment [x,y] then you can transform it either into [x−1,y] or into [x,y+1] .
Let's define a total intersection I 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 0 and length of a segment [x,y] is equal to y−x .
What is the minimum number of steps you need to make I greater or equal to k ?
输入格式
The first line contains the single integer t ( 1≤t≤1000 ) — the number of test cases.
The first line of each test case contains two integers n and k ( 1≤n≤2⋅105 ; 1≤k≤109 ) — the length of lists and the minimum required total intersection.
The second line of each test case contains two integers l1 and r1 ( 1≤l1≤r1≤109 ) — the segment all [ali,ari] are equal to initially.
The third line of each test case contains two integers l2 and r2 ( 1≤l2≤r2≤109 ) — the segment all [bli,bri] are equal to initially.
It's guaranteed that the sum of n doesn't exceed 2⋅105 .
输出格式
Print t integers — one per test case. For each test case, print the minimum number of step you need to make I greater or equal to k .
输入输出样例
输入#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 5 , for example, using next strategy:
- make [al1,ar1] from [1,2] to [1,4] in 2 steps;
- make [al2,ar2] from [1,2] to [1,3] in 1 step;
- make [bl1,br1] from [3,4] to [1,4] in 2 steps;
- make [bl2,br2] from [3,4] to [1,4] in 2 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] in 1000000000 steps and [bl1,br1]=[0,1000000000] in 1000000000 steps.
In the third test case, the total intersection I is already equal to 10>3 , so we don't need to do any steps.