01字典树板子题
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <deque>
//#include <windows.h>
using namespace std;
int two[40],tt;
long long mods[40];
struct node
{
int zero,one;
}tree[5000000];
void in(int step,int now)
{
if(step==0) return;
if(two[step]==0)
{
if(tree[now].zero==0)
{
tt++;
tree[now].zero=tt;
}
in( step-1, tree[now].zero);
}
if(two[step]==1)
{
if(tree[now].one==0)
{
tt++;
tree[now].one=tt;
}
in( step-1, tree[now].one);
}
}
long long anss(int step,int now)
{
if(step==0) return 0;
if(two[step]==0)
{
if(tree[now].one!=0)
{
return mods[step]+anss( step-1, tree[now].one);
}
else
{
return anss( step-1, tree[now].zero);
}
}
if(two[step]==1)
{
if(tree[now].zero!=0)
{
return anss( step-1, tree[now].zero);
}
else
{
return mods[step]+anss( step-1, tree[now].one);
}
}
}
int main()
{
int i,t,j,k,l,m,n,lt,jj;
scanf("%d",&t);
mods[0]=0;
mods[1]=1;
for(i=2;i<=32;i++)
mods[i]=mods[i-1]*2;
for(lt=1;lt<=t;lt++)
{
scanf("%d%d",&n,&m);
memset(tree,0,sizeof tree);
tt=1;
for(i=0;i<n;i++)
{
scanf("%d",&k);
for(j=1;j<=32;j++)
{
two[j]=k%2;
k/=2;
}
in(32,1);
}
printf("Case #%d:\n",lt);
for(i=0;i<m;i++)
{
scanf("%d",&k);
for(j=1;j<=32;j++)
{
two[j]=k%2;
k/=2;
}
printf("%lld\n",anss(32,1));
}
}
return 0;
}