hdu 4825

来源:互联网 发布:知识产权 淘宝 编辑:程序博客网 时间:2024/04/26 01:24
//我自创的字典树哦:)#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <iostream>#include <fstream>using namespace std;const int N=100010;const long long bit=2147483648;struct node{    int l,r;    node(): l(0), r(0) {}}tre[40*N];int num=0;void add(long long x){    long long a[32];    memset(a,0,sizeof(a));    int tmp,i=31;    while( x ){        a[i--]=x%2;        x/=2;    }    tmp=0;    for(i=0;i<32;i++)    {        if( a[i]==0 && !tre[tmp].l ){            tre[tmp].l=++num;            tmp=num;        }        else if( a[i]==1 && !tre[tmp].r ){            tre[tmp].r=++num;            tmp=num;        }        else{            tmp=(a[i]==0 ? tre[tmp].l:tre[tmp].r);        }    }}long long getAns(long long x){    long long a[32],ans,BIT;    memset(a,0,sizeof(a));    int tmp,i=31;    while( x ){        a[i--]=x%2;        x/=2;    }    tmp=0; ans=0; BIT=bit;    for(i=0;i<32;i++)    {        if( a[i]==0 )        {            if( tre[tmp].r>0 ){                ans+=BIT;                tmp=tre[tmp].r;            }            else tmp=tre[tmp].l;        }        else        {            if( tre[tmp].l>0 ){                tmp=tre[tmp].l;            }            else            {                tmp=tre[tmp].r;                ans+=BIT;            }        }        BIT/=2;    }    return ans;}int main(){//    freopen("in","r",stdin);//    freopen("out","w",stdout);    int t,i,j,n,m;    long long x,ans;    scanf("%d",&t);    for(i=1;i<=t;i++)    {        scanf("%d%d",&n,&m);        num=0;        for(j=0;j<=32*n;j++)            tre[j].l=tre[j].r=0;        while( n-- )        {            scanf("%I64d",&x);            add(x);        }        printf("Case #%d:\n",i);        while( m-- )        {            scanf("%I64dd",&x);            ans=getAns(x);            printf("%I64d\n",ans);        }    }    return 0;}

0 0
原创粉丝点击