CF2267F2.XOR Transformations (Hard Version)
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The difference between the versions is that in this version, the constraints on n and q are higher. You can make hacks only if you have solved all versions of this problem.
For an array b consisting of m integers, define a transformation as follows:
- Write down the values bi⊕bj for all 1≤i<j≤m, where ⊕ denotes the bitwise XOR operation.
- Take the m smallest among the written values.
- Replace the elements of the array with the taken m values.
For example, consider the transformation of the array [6,7,8,9,15]. We write down the values 1,1,6,7,8,9,14,14,15,15. After the transformation, the array becomes [1,1,6,7,8] — the 5 smallest elements.
You are given an array a consisting of n non-negative integers. Let max(a) denote the maximum element of the array a, and min(a) — the minimum. Your task is to answer q queries, each of which gives you one integer x. For each query, find the value of max(a)−min(a) after x transformations on the array. Note that the queries are independent, i.e. before each query, the array a is restored to its original state.
这是该问题的困难版本。两个版本的区别在于,本版本中 n 和 q 的约束更大。仅当您已解决该问题的所有版本后,才可进行 Hack。
对于一个由 m 个整数组成的数组 b,定义如下变换:
- 写出所有满足 1≤i<j≤m 的 bi⊕bj 的值,其中 ⊕ 表示按位异或(XOR)运算;
- 取出所写出的值中最小的 m 个;
- 将数组的元素替换为这 m 个选出的值。
例如,考虑对数组 [6,7,8,9,15] 进行变换:我们写出所有值 1,1,6,7,8,9,14,14,15,15。变换后,数组变为 [1,1,6,7,8] —— 即其中最小的 5 个元素。
给定一个由 n 个非负整数组成的数组 a。记 max(a) 为数组 a 的最大值,min(a) 为其最小值。您的任务是回答 q 个查询,每个查询给出一个整数 x。对每个查询,请计算对数组 a 执行 x 次上述变换后 max(a)−min(a) 的值。注意,各查询相互独立,即每次查询前,数组 a 都恢复为其原始状态。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and q (5≤n≤105,1≤q≤105) — the size of the array and the number of queries.
The second line of each test case contains n integers a1,a2,…,an (0≤ai<230).
The next q lines of each test case contain an integer x (0≤x<230).
It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 q(5≤n≤105,1≤q≤105)——分别表示数组的大小和查询次数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(0≤ai<230)。
每个测试用例接下来的 q 行,每行包含一个整数 x(0≤x<230)。
保证所有测试用例中 n 的总和与 q 的总和均不超过 105。
输出格式
For each test case, print q integers — the answer to each query.
对于每个测试用例,输出 q 个整数——即每个查询的答案。
输入输出样例
输入#1
4 5 1 0 0 1 1 1 1 5 2 6 7 8 9 15 0 1 8 2 102 92 15 19 25 54 62 36 2 1 10 1 56 73 81 23 17 92 50 34 67 78 2
输出#1
1 9 7 12 31 7
说明/提示
In the first test case, after one transformation, the array becomes [0,0,0,0,1]. Here max(a)−min(a)=1−0=1.
In the second test case, the initial array is [6,7,8,9,15]. Initially, max(a)−min(a)=15−6=9. After the transformation, the array becomes [1,1,6,7,8], where max(a)−min(a)=8−1=7.
在第一个测试用例中,经过一次变换后,数组变为 [0,0,0,0,1]。此时 max(a)−min(a)=1−0=1。
在第二个测试用例中,初始数组为 [6,7,8,9,15]。初始时,max(a)−min(a)=15−6=9。变换后,数组变为 [1,1,6,7,8],此时 max(a)−min(a)=8−1=7。
输入解题思路,AI测评打分。不知道怎么写?