CF1883G2.Dances (Hard Version)
普及+/提高
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference is that in this version m≤109.
You are given two arrays of integers a1,a2,…,an and b1,b2,…,bn. 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 ai<bi for all 1≤i≤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≤i≤m. Array c[i] is obtained from a as follows:
- c[i]1=i,
- c[i]j=aj, for 2≤j≤n.
这是该问题的困难版本。唯一的区别在于本版本中 m≤109。
给你两个整数数组 a1,a2,…,an 和 b1,b2,…,bn。在执行任何操作之前,你可以任意重排每个数组中的元素。然后,在一次操作中(当两个数组均非空时),你需同时执行以下两个动作:
- 从数组 a 中任选一个元素并将其删除(其余所有元素向左移动,构成新的数组 a);
- 从数组 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。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) - 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≤n≤105, 1≤m≤109) - the size of arrays a and b and the constraints on the value of element a1.
The second line of each test case contains n−1 integers a2,…,an (1≤ai≤109).
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤109).
It is guaranteed that the sum of n over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示输入数据组数。随后是各组数据的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n≤105,1≤m≤109),分别表示数组 a 和 b 的长度,以及对元素 a1 的取值约束。
每个测试用例的第二行包含 n−1 个整数 a2,…,an(1≤ai≤109)。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤109)。
保证所有测试用例中 n 的总和不超过 105。
输出格式
For each test case, output the total number of minimum operations for all pairs of arrays (ci,b).
对于每个测试用例,输出所有数组对 (ci,b) 所需的最少操作总数。
输入输出样例
输入#1
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
输出#1
2 12 16 4
说明/提示
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.
在第一个测试用例中:
- 对于数组对 ([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。
输入解题思路,AI测评打分。不知道怎么写?