您的位置:首页 > 其它

Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson

2017-09-18 21:46 549 查看
因为抑或,一眼字典树

但是处理起来比较难

#include<iostream>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 6e6+5;
#define MS(x,y) memset(x,y,sizeof(x))
#define MP(x, y) make_pair(x, y)
const int INF = 0x3f3f3f3f;

int nx
[2];
int cnt
;
int has
;
int tot;
void insert(int x) {
int rt = 0;
for(int i = 18; i >= 0; --i) {
int tt = (x>>i) & 1;
//  printf("%d\n", rt);
if(!nx[rt][tt]) {
nx[rt][tt] = ++tot;
}
rt = nx[rt][tt];
cnt[rt] ++;
}
//  has[rt] = 1;
}
void search(int x) {
int rt = 0;
int ans = 0;
for(int i = 18; i >= 0; --i) {
int tt = (x >> i) & 1;
if( (1<<i) - cnt[nx[rt][0 ^ tt]] ) {
if(cnt[nx[rt][0 ^ tt]] == 0) break;
rt = nx[rt][0 ^ tt];
}else {
if(cnt[nx[rt][1 ^ tt]] == 0) {
//  printf("hh\n");
ans += 1<<i;
break;
}

rt = nx[rt][1 ^ tt]; ans += 1<<i;
}
//  printf("%d %d\n", rt, 0^tt);
}
printf("%d\n", ans);
}
int main() {
int n, m;
while(~scanf("%d %d", &n, &m)) {
MS(nx, 0);
MS(cnt, 0);
tot = 0;
map<int, int> mp;
for(int i = 1; i <= n; ++i) {
int a; scanf("%d", &a);
if(mp.find(a) == mp.end()) insert(a);
mp[a] ++;
}
int tmp = 0;
for(int i = 0; i < m; ++i) {
int a; scanf("%d", &a);
tmp ^= a;
search(tmp);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: