D. Eating
D. Eating
There are slimes on a line, the -th of which has weight . Slime is able to eat another slime if ; afterwards, slime disappears and the weight of slime becomes .
The King of Slimes wants to run an experiment with parameter as follows:
- Add a new slime with weight to the right end of the line (after the -th slime).
- This new slime eats the slime to its left if it is able to, and then takes its place (moves one place to the left). It will continue to do this until there is either no slime to its left or the weight of the slime to its left is greater than its own weight. (No other slimes are eaten during this process.)
- The score of this experiment is the total number of slimes eaten.
The King of Slimes is going to ask you queries. In each query, you will be given an integer , and you need to determine the score of the experiment with parameter .
Note that the King does not want you to actually perform each experiment; his slimes would die, which is not ideal. He is only asking what the hypothetical score is; in other words, the queries are not persistent.
Here denotes the bitwise XOR operation.
Input
The first line contains an integer () — the number of test cases.
The first line of each test case contains integers and () — the number of slimes and the number of queries, respectively.
The following line contains integers () — the weights of the slimes.
The following lines contain a single integer () — the parameter for the experiment.
The sum of does not exceed and the sum of does not exceed across all test cases.
Output
For each query, output a single integer — the score of the experiment.
Example
Input
3
1 1
5
6
4 4
1 5 4 11
8
13
16
15
10 9
10 4 3 9 7 4 6 1 9 4
2
6
5
6
9
8
6
2
7
Output
1
0 2 4 2
0 1 1 1 3 3 1 0 1
Note
For the first query of the second testcase:
- A slime of weight would be added to the end, so .
- The added slime has smaller weight than the slime to its left so it cannot eat it, and thus ends the process after eating no slimes with score .
For the second query of the second testcase:
- A slime of weight would be added to the end, so .
- The added slime has bigger weight than the slime to its left, and so it will eat it. Its weight will become . Now .
- The added slime will now eat the slime to its left, and its weight becomes . Now .
- The added slime is no longer able to eat the slime to its left, so it ends the process with a score of .
解题思路
开始一直想着找到最小的 使得对于每个 满足 ,结果做不出来。实际上只要注意到当 时,必然存在一个最高的二进制位 是 而 是 。
为此我们只需关注 的最高有效位即可。假设 的最高有效位是第 位,如果所有的 的最高有效位都严格小于 ,那么一定是可以吃掉所有的 。如果存在存在某个 的最高有效位严格大于 ,那么 及其前面的都一定吃不到。而如果某个 的最高有效位恰好等于 ,只有满足 才能吃掉,并且 的最高有效位在异或了 后将变小,且在之后的过程中第 位永远不会变成 。意味着在整个过程中, 的最高有效位是不断变小的,且最多变化 次。
先预处理一个数组 表示第 个位置左边第一个最高有效位大于等于 的元素位置, 表示前缀的异或和即 。定义 表示当前所在的位置(还没吃掉 ),初始时 。整个过程就是能吃就吃。
- 判断是否满足 ,如果不满足则结束整个过程。
- 吃掉 ,有 ,。
- 假设 的最高有效位为 ,那么左边第一个最高有效位大于等于 的元素位置就是 ,那么吃掉 ,并更新 ,。
- 如果 或者 则结束整个过程,否则回到第 步。
最后能吃到的数量就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int a[N];
int l[30][N], s[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < 30; i++) {
for (int j = 1; j <= n; j++) {
if (__lg(a[j]) >= i) l[i][j] = j;
else l[i][j] = l[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] ^ a[i];
}
while (m--) {
int x;
cin >> x;
int ret = 0, t = n;
while (x && t) {
if (a[t] > x) break;
if (!(x ^ a[t])) {
ret++;
break;
}
int k = l[__lg(x ^ a[t])][t - 1];
ret += t - k;
x ^= s[t] ^ s[k];
t = k;
}
cout << ret << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round 1005 (Div. 2) Editorial:https://codeforces.com/blog/entry/138912
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18722840
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2024-02-18 F - Breakdown
2023-02-18 D. Triangle Coloring
2023-02-18 约数之和
2022-02-18 地宫取宝