CF1821D.Black Cells

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are playing with a really long strip consisting of 101810^{18} white cells, numbered from left to right as 00 , 11 , 22 and so on. You are controlling a special pointer that is initially in cell 00 . Also, you have a "Shift" button you can press and hold.

In one move, you can do one of three actions:

  • move the pointer to the right (from cell xx to cell x+1x + 1 );
  • press and hold the "Shift" button;
  • release the "Shift" button: the moment you release "Shift", all cells that were visited while "Shift" was pressed are colored in black.

(Of course, you can't press Shift if you already hold it. Similarly, you can't release Shift if you haven't pressed it.)Your goal is to color at least kk cells, but there is a restriction: you are given nn segments [li,ri][l_i, r_i] — you can color cells only inside these segments, i. e. you can color the cell xx if and only if lixril_i \le x \le r_i for some ii .

What is the minimum number of moves you need to make in order to color at least kk cells black?

输入格式

The first line contains a single integer tt ( 1t1041 \le t \le 10^4 ) — the number of test cases.

The first line of each test case contains two integers nn and kk ( 1n21051 \le n \le 2 \cdot 10^5 ; 1k1091 \le k \le 10^9 ) — the number of segments and the desired number of black cells, respectively.

The second line contains nn integers l1,l2,,lnl_1, l_2, \dots, l_n ( 1l1<l2<<ln1091 \le l_1 < l_2 < \dots < l_n \le 10^9 ), where lil_i is the left border of the ii -th segment.

The third line contains nn integers r1,r2,,rnr_1, r_2, \dots, r_n ( 1ri1091 \le r_i \le 10^9 ; liri<li+11l_i \le r_i < l_{i + 1} - 1 ), where rir_i is the right border of the ii -th segment.

Additional constraints on the input:

  • every cell belongs to at most one segment;
  • the sum of nn doesn't exceed 21052 \cdot 10^5 .

输出格式

For each test case, print the minimum number of moves to color at least kk cells black, or 1-1 if it's impossible.

输入输出样例

  • 输入#1

    4
    2 3
    1 3
    1 4
    4 20
    10 13 16 19
    11 14 17 20
    2 3
    1 3
    1 10
    2 4
    99 999999999
    100 1000000000

    输出#1

    8
    -1
    7
    1000000004

说明/提示

In the first test case, one of the optimal sequences of operations is the following:

  1. Move right: pointer is moving into cell 11 ;
  2. Press Shift;
  3. Release Shift: cell 11 is colored black;
  4. Move right: pointer is moving into cell 22 ;
  5. Move right: pointer is moving into cell 33 ;
  6. Press Shift;
  7. Move right: pointer is moving into cell 44 ;
  8. Release Shift: cells 33 and 44 are colored in black.

We've colored 33 cells in 88 moves.In the second test case, we can color at most 88 cells, while we need 2020 cell to color.

In the third test case, one of the optimal sequences of operations is the following:

  1. Move right: pointer is moving into cell 11 ;
  2. Move right: pointer is moving into cell 22 ;
  3. Move right: pointer is moving into cell 33 ;
  4. Press Shift;
  5. Move right: pointer is moving into cell 44 ;
  6. Move right: pointer is moving into cell 55 ;
  7. Release Shift: cells 33 , 44 and 55 are colored in black.

We've colored 33 cells in 77 moves.

首页