Vitya and Strange Lesson
思路
树专题,所以一定是用
树来写了,我们考虑,对原始数组,如何通过
树来查找最小值。
显然我们可以把,不在数组中的所有数字加入树,然后通过查找与0异或最小的数即为答案。
这题显然我们也可以同样利用这一性质,先把不在数组中的所有数加入树,然后再通过查找与所有
异或起来最小的数即可。
代码
/*
Author : lifehappy
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int trie[N][2], tot;
int vis[600000 + 10], n, m;
void add(int x) {
int rt = 0;
for(int i = 20; i >= 0; i--) {
int now = x >> i & 1;
if(!trie[rt][now]) trie[rt][now] = ++tot;
rt = trie[rt][now];
}
}
int query(int x) {
int ans = 0, rt = 0;
for(int i = 20; i >= 0; i--) {
int now = x >> i & 1;
if(trie[rt][now]) {
rt = trie[rt][now];
}
else {
ans |= 1 << i;
rt = trie[rt][now ^ 1];
}
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
vis[x]++;
}
for(int i = 0; i < 600010; i++) {
if(!vis[i]) {
add(i);
}
}
int ans = 0;
for(int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
ans ^= x;
printf("%d\n", query(ans));
}
return 0;
} 
京公网安备 11010502036488号