hdu 4825 Xor Sum(01字典树模版题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825

 

题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异或处理。

 

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int M = 1e5 + 10;
int ch[33 * M][2];
int node_size;
ll val[33 * M];
void init() {
    node_size = 1;
    ch[0][0] = ch[0][1] = 0;
}
void Insert(ll x) {
    int now = 0;
    for(int i = 32 ; i >= 0 ; i--) {
        int id = (x >> i) & 1;
        if(!ch[now][id]) {
            ch[node_size][0] = ch[node_size][1] = 0;
            ch[now][id] = node_size;
            val[node_size++] = 0;
        }
        now = ch[now][id];
    }
    val[now] = x;
}
ll query(ll x) {
    int now = 0;
    for(int i = 32 ; i >= 0 ; i--) {
        int id = (x >> i) & 1;
        if(ch[now][id ^ 1]) {
            now = ch[now][id ^ 1];
        }
        else {
            now = ch[now][id];
        }
    }
    return val[now];
}
int main() {
    int t , Case = 0;
    scanf("%d" , &t);
    while(t--) {
        int n , m , x;
        scanf("%d%d" , &n , &m);
        init();
        for(int i = 0 ; i < n ; i++) {
            scanf("%d" , &x);
            Insert(x);
        }
        printf("Case #%d:\n" , ++Case);
        while(m--) {
            scanf("%d" , &x);
            printf("%lld\n" , query(x));
        }
    }
    return 0;
}

 

posted @ 2017-09-12 16:59  Gealo  阅读(137)  评论(0编辑  收藏  举报