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

因为抑或,一眼字典树
但是处理起来比较难

#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[N][2];
int cnt[N];
int has[N];
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;
}
posted @ 2017-09-18 21:46  basasuya  阅读(126)  评论(0编辑  收藏  举报