HDU 4825 Xor Sum

Xor Sum

中文题意不解释。

题解:就是开一个01字典树,从最高位开始, 尽可能的去找到当前位取反的值, 然后就可以找到结果。 结果怕每次初始化然后TLE, 手残写了一个垃圾优化, 然后疯狂WA 2333,最后还是初始化了。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 #define min3(a,b,c) min(a,min(b,c))
11 const int INF = 0x3f3f3f3f;
12 const LL mod = 1e9+7;
13 typedef pair<int,int> pll;
14 const int N = 4000010;
15 int tree[N][2];
16 int tot = 2;
17 void Insert(int tmp){
18     int rt = 1;
19     for(int i = 31; i >= 0; i--){
20         int id = (tmp>>i)&1;
21         if(tree[rt][id] == 0) tree[rt][id] = tot++;
22         rt = tree[rt][id];
23     }
24 }
25 int Find(int tmp){
26     int rt = 1;
27     int ret = 0;
28     for(int i = 31; i >= 0; i--){
29         int id = (tmp>>i)&1;
30         if(tree[rt][!id] != 0) {
31             rt = tree[rt][!id];
32             ret += 1 << i;
33 
34         }
35         else{
36             rt = tree[rt][id];
37         }
38     }
39     return ret^tmp;
40 }
41 int main(){
42     int T, n, m, cas = 1;
43     int tmp;
44     scanf("%d", &T);
45     while(T--){
46         memset(tree, 0, sizeof(tree));
47         scanf("%d%d",&n,&m);
48         printf("Case #%d:\n", cas++);
49         tot = 2;
50         for(int i = 1; i <= n; i++) {
51             scanf("%d", &tmp);
52             Insert(tmp);
53         }
54         for(int i = 1; i <= m; i++){
55             scanf("%d", &tmp);
56             printf("%d\n",Find(tmp));
57         }
58     }
59 
60     return 0;
61 }
View Code

 

posted @ 2018-04-03 17:33  Schenker  阅读(204)  评论(0编辑  收藏  举报