不再根据son[p][u]
来走,而是根据cnt[son[p][u]]
。cnt记录经过此节点的数的数量
#include<iostream>
using namespace std;
const int N = 1e5 * 31 + 10, M = 1e5 + 10;
int son[N][2], idx, cnt[N], sum[M];
int n, m;
void insert(int x, int op){
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] += op;
}
}
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(){
cin >> n >> m;
for (int i = 1; i <= n; i ++ ){
cin >> sum[i];
sum[i] ^= sum[i - 1];
}
insert(sum[0], 1);
int res = -1;
for (int i = 1; i <= n; i ++ ){
if (i > m) insert(sum[i - m - 1], -1);
res = max(res, query(sum[i]));
insert(sum[i], 1);
}
cout << res << endl;
return 0;
}