01字典树

 (orz从之前从来没见过这种题

Xor Sum

HDU - 4825
模板~
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 const int maxnode = 100010 * 32;
 5 const int sigma = 2;
 6 struct AC01{
 7     int ch[maxnode][sigma];
 8     LL val[maxnode];
 9     int sz;
10     void init(){
11         sz = 1;
12         val[0] = 0;
13         memset(ch[0], 0, sizeof(ch[0]));
14     }
15     void add(LL x){
16         int u = 0, n = 32;
17         for(int i = n; i >= 0; i--){
18             int c = (x >> i) & 1;
19             if(!ch[u][c]){
20                 memset(ch[sz], 0, sizeof(ch[sz]));
21                 val[sz] = 0;
22                 ch[u][c] = sz++;
23             }
24             u = ch[u][c];
25         }
26         val[u] = x;
27     }
28     //返回与x异或最大的值val[u];
29     LL query(LL x){
30         int u = 0, n = 32;
31         for(int i = n; i >= 0; i--){
32             int c = (x >> i) & 1;
33             if(ch[u][c ^ 1]) u = ch[u][c ^ 1];
34             else u = ch[u][c];
35         }
36         return val[u];
37     }
38 }ac;
39 int main(){
40     int t, kase = 0;
41     //freopen("in.txt", "r", stdin);
42     scanf("%d", &t);
43     while(t--){
44         ac.init();
45         int n, q;
46         scanf("%d %d", &n, &q);
47         for(int i = 0; i < n; i++) {
48             LL x;
49             scanf("%lld", &x);
50             ac.add(x);
51         }
52         printf("Case #%d:
", ++kase);
53         while(q--){
54             LL x;
55             scanf("%lld", &x);
56             LL temp = ac.query(x);
57             printf("%lld
", temp);
58         }
59     }
60 }
View Code

Chip Factory

HDU - 5536
带删除的, num jiluyixia
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 const int maxnode = 1010 * 32;
 5 const int sigma = 2;
 6 struct AC01{
 7     int ch[maxnode][sigma];
 8     int val[maxnode];
 9     int num[maxnode];
10     int sz;
11     void init(){
12         sz = 1;
13         val[0] = 0;
14         num[0] = 0;
15         memset(ch[0], 0, sizeof(ch[0]));
16     }
17     void add(int x){
18         int u = 0, n = 31;
19         for(int i = n; i >= 0; i--){
20             int c = (x >> i) & 1;
21             if(!ch[u][c]){
22                 memset(ch[sz], 0, sizeof(ch[sz]));
23                 val[sz] = 0;
24                 num[sz] = 0;
25                 ch[u][c] = sz++;
26             }
27             u = ch[u][c];
28             num[u]++;
29         }
30         val[u] = x;
31     }
32     //
33     void update(int x, int d){
34         int u = 0, n = 31;
35         for(int i = n; i >= 0; i--){
36             int c = (x >> i) & 1;
37             u = ch[u][c];
38             num[u] += d;
39         }
40     }
41     //返回与x异或最大的值val[u];
42     int query(int x){
43         int u = 0, n = 31;
44         for(int i = n; i >= 0; i--){
45             int c = (x >> i) & 1;
46             if(ch[u][c ^ 1] && num[ch[u][c ^ 1]]) u = ch[u][c ^ 1];
47             else u = ch[u][c];
48         }
49         return val[u];
50     }
51 }ac;
52 int x[1010];
53 int main(){
54     int t, kase = 0;
55     //freopen("in.txt", "r", stdin);
56     scanf("%d", &t);
57     while(t--){
58         ac.init();
59         int n, q;
60         scanf("%d", &n);
61         for(int i = 0; i < n; i++) {
62             scanf("%d", &x[i]);
63             ac.add(x[i]);
64         }
65         int ans = 0;
66         for(int i = 0; i < n; i++){
67             for(int j = i + 1; j < n; j++){
68                 ac.update(x[i], -1);
69                 ac.update(x[j], -1);
70                 ans = max(ans, (x[i] + x[j]) ^ ac.query(x[i] + x[j]));
71                 ac.update(x[i], 1);
72                 ac.update(x[j], 1);
73             }
74         }
75         printf("%d
", ans);
76     }
77 }
View Code

The xor-longest Path

POJ - 3764

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 #define LL long long
 7 const int maxnode = 100010 * 32;
 8 const int sigma = 2;
 9 const int maxn = 100010;
10 struct Edge{
11     int v, w;
12     int nex;
13     Edge(int v = 0, int w = 0, int nex = 0) : v(v), w(w), nex(nex) {}
14 }e[maxn<<1];
15 int head[maxn];
16 int cnt;
17 void init(){
18     memset(head, -1, sizeof(head));
19     cnt = 0;
20 }
21 void add(int u, int v, int w){
22     e[cnt] = Edge(v, w, head[u]);
23     head[u] = cnt++;
24 }
25 
26 struct AC01{
27     int ch[maxnode][sigma];
28     int val[maxnode];
29     int sz;
30     void init(){
31         sz = 1;
32         val[0] = 0;
33         memset(ch[0], 0, sizeof(ch[0]));
34     }
35     void insert(int x){
36         int u = 0, n = 31;
37         for(int i = n; i >= 0; i--){
38             int c = (x >> i) & 1;
39             if(!ch[u][c]){
40                 memset(ch[sz], 0, sizeof(ch[sz]));
41                 val[sz] = 0;
42                 ch[u][c] = sz++;
43             }
44             u = ch[u][c];
45         }
46         val[u] = x;
47     }
48     //返回与x异或最大的值val[u];
49     int query(int x){
50         int u = 0, n = 31;
51         for(int i = n; i >= 0; i--){
52             int c = (x >> i) & 1;
53             if(ch[u][c ^ 1]) u = ch[u][c ^ 1];
54             else u = ch[u][c];
55         }
56         return val[u] ^ x;  //
57     }
58 }ac;
59 int ans;
60 
61 void dfs(int u, int f, int c){
62     ac.insert(c);
63     for(int i = head[u]; ~i; i = e[i].nex){
64         int v = e[i].v;
65         if(v == f) continue;
66         ans = max(ans, ac.query(c ^ e[i].w));
67         dfs(v, u, c ^ e[i].w);
68     }
69 }
70 
71 int main(){
72     int n;
73    //freopen("in.txt", "r", stdin);
74     while(scanf("%d", &n) != EOF){
75         int u, v, w;
76         ans = 0;
77         init();
78         for(int i = 1; i < n; i++){
79             scanf("%d %d %d", &u, &v, &w);
80             add(u, v, w);
81             add(v, u, w);
82         }
83         ac.init();
84         dfs(0, -1, 0);
85         printf("%d
", ans);
86     }
87     return 0;
88 }
View Code

Codechef REBXOR

HYSBZ - 4260
 
 
原文地址:https://www.cnblogs.com/yijiull/p/7768089.html