310782: CF1887A2. Dances (Hard Version)

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A2. Dances (Hard Version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

This is the hard version of the problem. The only difference is that in this version $m \leq 10^9$.

You are given two arrays of integers $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty:

  • Choose any element from array $a$ and remove it (all remaining elements are shifted to a new array $a$),
  • Choose any element from array $b$ and remove it (all remaining elements are shifted to a new array $b$).

Let $k$ be the final size of both arrays. You need to find the minimum number of operations required to satisfy $a_i < b_i$ for all $1 \leq i \leq k$.

This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer $m$. Now, you need to find the sum of answers to the problem for $m$ pairs of arrays $(c[i], b)$, where $1 \leq i \leq m$. Array $c[i]$ is obtained from $a$ as follows:

  • $c[i]_1 = i$,
  • $c[i]_j = a_j$, for $2 \leq j \leq n$.
Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) - the number of sets of input data. This is followed by their description.

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 10^5$, $1 \leq m \leq 10^9$) - the size of arrays $a$ and $b$ and the constraints on the value of element $a_1$.

The second line of each test case contains $n - 1$ integers $a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq 10^9$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output

For each test case, output the total number of minimum operations for all pairs of arrays $(c_i, b)$.

ExampleInput
4
2 4
1
3 2
4 7
5 1 5
3 8 3 3
8 4
4 3 3 2 2 1 1
1 1 1 1 3 3 3 3
9 1
9 2 8 3 7 4 6 5
1 2 3 2 1 4 5 6 5
Output
2
12
16
4
Note

In the first test case:

  • For the pair of arrays $([1, 1], [3, 2])$, the answer is $0$. No operations or reordering of elements are needed.
  • For the pair of arrays $([2, 1], [3, 2])$, the answer is $0$. The elements of the first array can be rearranged to obtain $[1, 2)$. No operations are needed.
  • For the pair of arrays $([3, 1], [3, 2])$, the answer is $1$. The element $3$ can be removed from the first array and the element $2$ can be removed from the second array.
  • For the pair of arrays $([4, 1], [3, 2])$, the answer is $1$. The element $4$ can be removed from the first array and the element $3$ can be removed from the second array.

Output

题目大意:
这个题目是“舞蹈(困难版本)”。在这个问题中,你被给予两个整数数组a和b,数组大小均为n。在进行任何操作之前,你可以按照任意顺序重新排列每个数组的元素。然后,每次操作你可以执行以下两个行为:

1. 从数组a中移除任意一个元素(剩下的元素会移动到新的数组a中),
2. 从数组b中移除任意一个元素(剩下的元素会移动到新的数组b中)。

设k为最终两个数组的共同大小。你需要找到最小的操作次数,使得对于所有1≤i≤k,都有ai < bi。

为了增加问题的挑战性,还给你一个正整数m。现在,你需要找到m对数组(c[i], b)的答案之和,其中1≤i≤m。数组c[i]从a获得如下:

- c[i]1 = i,
- c[i]j = aj,对于2≤j≤n。

输入输出数据格式:
输入:
- 第一行包含一个整数t(1≤t≤10^4),表示输入数据集的数量。
- 每个数据集的描述如下:
- 第一行包含两个整数n和m(2≤n≤10^5,1≤m≤10^9),表示数组a和b的大小以及a1的约束。
- 第二行包含n-1个整数a2, …, an(1≤ai≤10^9)。
- 第三行包含n个整数b1, b2, …, bn(1≤bi≤10^9)。
- 保证所有测试用例的n之和不超过10^5。

输出:
- 对于每个测试用例,输出所有数组对(c[i], b)的最小操作总数。

示例输入输出:
输入:
```
4
2 4
1
3 2
4 7
5 1 5
3 8 3 3
8 4
4 3 3 2 2 1 1
1 1 1 1 3 3 3 3
9 1
9 2 8 3 7 4 6 5
1 2 3 2 1 4 5 6 5
```
输出:
```
2
12
16
4
```

注意:
- 在第一个测试用例中,对于数组对([1, 1], [3, 2]),答案为0。不需要任何操作或重新排序元素。
- 对于数组对([2, 1], [3, 2]),答案为0。第一个数组的元素可以重新排列为[1, 2]。不需要任何操作。
- 对于数组对([3, 1], [3, 2]),答案为1。可以从第一个数组中移除元素3,并从第二个数组中移除元素2。
- 对于数组对([4, 1], [3, 2]),答案为1。可以从第一个数组中移除元素4,并从第二个数组中移除元素3。题目大意: 这个题目是“舞蹈(困难版本)”。在这个问题中,你被给予两个整数数组a和b,数组大小均为n。在进行任何操作之前,你可以按照任意顺序重新排列每个数组的元素。然后,每次操作你可以执行以下两个行为: 1. 从数组a中移除任意一个元素(剩下的元素会移动到新的数组a中), 2. 从数组b中移除任意一个元素(剩下的元素会移动到新的数组b中)。 设k为最终两个数组的共同大小。你需要找到最小的操作次数,使得对于所有1≤i≤k,都有ai < bi。 为了增加问题的挑战性,还给你一个正整数m。现在,你需要找到m对数组(c[i], b)的答案之和,其中1≤i≤m。数组c[i]从a获得如下: - c[i]1 = i, - c[i]j = aj,对于2≤j≤n。 输入输出数据格式: 输入: - 第一行包含一个整数t(1≤t≤10^4),表示输入数据集的数量。 - 每个数据集的描述如下: - 第一行包含两个整数n和m(2≤n≤10^5,1≤m≤10^9),表示数组a和b的大小以及a1的约束。 - 第二行包含n-1个整数a2, …, an(1≤ai≤10^9)。 - 第三行包含n个整数b1, b2, …, bn(1≤bi≤10^9)。 - 保证所有测试用例的n之和不超过10^5。 输出: - 对于每个测试用例,输出所有数组对(c[i], b)的最小操作总数。 示例输入输出: 输入: ``` 4 2 4 1 3 2 4 7 5 1 5 3 8 3 3 8 4 4 3 3 2 2 1 1 1 1 1 1 3 3 3 3 9 1 9 2 8 3 7 4 6 5 1 2 3 2 1 4 5 6 5 ``` 输出: ``` 2 12 16 4 ``` 注意: - 在第一个测试用例中,对于数组对([1, 1], [3, 2]),答案为0。不需要任何操作或重新排序元素。 - 对于数组对([2, 1], [3, 2]),答案为0。第一个数组的元素可以重新排列为[1, 2]。不需要任何操作。 - 对于数组对([3, 1], [3, 2]),答案为1。可以从第一个数组中移除元素3,并从第二个数组中移除元素2。 - 对于数组对([4, 1], [3, 2]),答案为1。可以从第一个数组中移除元素4,并从第二个数组中移除元素3。

加入题单

上一题 下一题 算法标签: