标签:最大 des 游戏 min 不可用 ring 整数 space init
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1786 Accepted Submission(s): 758
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define clr(x) memset(x,0,sizeof(x)) 5 #define clrmin(x) memset(x,-1,sizeof(x)) 6 #define LL long long 7 using namespace std; 8 struct node 9 { 10 int lt,rt; 11 bool num; 12 LL val; 13 }; 14 struct Trie 15 { 16 int head,len; 17 node tr[500010]; 18 Trie () { clr(tr); head=0; len=1;} 19 void init() 20 { 21 clr(tr); 22 head=0; 23 len=1; 24 return ; 25 } 26 int newnode(LL num,int dep) 27 { 28 if(!head) 29 { 30 head=len; 31 } 32 tr[len].num=((num>>(32-dep))%2)^1; 33 return len++; 34 } 35 void push(int fa,int now,LL num,int dep) 36 { 37 int p; 38 if(!now) 39 { 40 now=newnode(num,dep); 41 tr[fa].lt=now; 42 if(dep==32) 43 tr[now].val=num; 44 else 45 push(now,0,num,dep+1); 46 return ; 47 } 48 while(now && (((num>>(32-dep))%2)^1)!=tr[now].num) 49 { 50 p=now; 51 now=tr[now].rt; 52 } 53 if(!now) 54 { 55 now=newnode(num,dep); 56 tr[p].rt=now; 57 } 58 if(dep==32) 59 tr[now].val=num; 60 else 61 push(now,tr[now].lt,num,dep+1); 62 return; 63 } 64 LL getxor(int now,LL num,int dep) 65 { 66 int p; 67 while(now && (num>>(32-dep))%2!=tr[now].num) 68 { 69 p=now; 70 now=tr[now].rt; 71 } 72 if(!now) 73 { 74 if(dep==32) 75 return tr[p].val; 76 else 77 return getxor(tr[p].lt,num,dep+1); 78 } 79 else 80 { 81 if(dep==32) 82 return tr[now].val; 83 else 84 return getxor(tr[now].lt,num,dep+1); 85 } 86 } 87 }tree; 88 int main() 89 { 90 int T,n,m; 91 LL num; 92 scanf("%d",&T); 93 for(int kase=1;kase<=T;kase++) 94 { 95 tree.init(); 96 printf("Case #%d:\n",kase); 97 scanf("%d%d",&n,&m); 98 for(int i=1;i<=n;i++) 99 { 100 scanf("%lld",&num); 101 tree.push(0,tree.head,num,0); 102 } 103 for(int i=1;i<=m;i++) 104 { 105 scanf("%lld",&num); 106 printf("%lld\n",tree.getxor(tree.head,num,0)); 107 } 108 } 109 return 0; 110 }
标签:最大 des 游戏 min 不可用 ring 整数 space init
原文地址:http://www.cnblogs.com/wujiechao/p/6224485.html