usaco training 6.1 牛异或 前缀异或和+异或字典树差不多的题目,只不过增加了异或字典树的删除。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010 * 31, M = 100010;
int n, m;
int s[M];
int son[N][2], cnt[N], idx;
void insert(int x, int v)
{
int p = 0;
for (int i = 30; i >= 0; i -- )
{
int u = x >> i & 1;
if (!son[p][u]) son[p][u] = ++ idx;
p = son[p][u];
cnt[p] += v;
}
}
int query(int x)
{
int res = 0, p = 0;
for (int i = 30; i >= 0; i -- )
{
int u = x >> i & 1;
if (cnt[son[p][!u]]) p = son[p][!u], res = res * 2 + 1;
else p = son[p][u], res = res * 2;
}
return res;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
{
int x;
scanf("%d", &x);
s[i] = s[i - 1] ^ x;
}
int res = 0;
insert(s[0], 1);
for (int i = 1; i <= n; i ++ )
{
if (i > m) insert(s[i - m - 1], -1);
res = max(res, query(s[i]));
insert(s[i], 1);
}
printf("%d\n", res);
return 0;
}