Xor Sum
(orz从之前从来没见过这种题
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:\n", ++kase); 53 while(q--){ 54 LL x; 55 scanf("%lld", &x); 56 LL temp = ac.query(x); 57 printf("%lld\n", temp); 58 } 59 } 60 }
时间: 2024-10-31 09:04:05