codeforces 842D. Vitya and Strange Lesson

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/My_stage/article/details/77725403

http://codeforces.com/contest/842/problem/D
思路:
我把数组中的数扔进字典树里,之后我记录下每个状态出现的次数。随后我们不需要去每次都把数组异或,我们把所有查询的数异或,

#include <bits/stdc++.h>
#define maxs 2002020
#define mme(i,j) memset(i,j,sizeof(i))
#define pb push_back
#define ll long long
#define lala puts("Lalalala")
#define ohMyLove ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const ll inf = LONG_LONG_MAX;
int son[maxs][3],val[maxs];
int rt,tot;
void init()
{
    rt=0;
    tot=1;
    mme(son[rt],0);
}

void Insert(int x)
{
    int now=rt,id;
    for(int i=20; i>=0; i--)
    {
        id = ((x>>i)&1);
        if(!son[now][id])
            son[now][id]=tot++;
        now = son[now][id];
        val[now]++;
    }
}

int query(int x)
{
    int now=rt,id,ans=0;
    for(int i=20; i>=0; i--)
    {
        id = ( (x>>i) & 1 );
        if(!son[now][id])
            return ans;

        if(val[son[now][id]]==(1<<i))
        {
            ans|=(1<<i);
            now = son[now][1-id];
        }
        else
            now = son[now][id];
    }
    return ans;
}

map<int,int>mp;

int main()
{
    ohMyLove;
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        mp.clear();
        int x;
        init();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&x);
            if(mp[x]==0)
                Insert(x);
            mp[x]=1;
        }

        int y=0;
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&x);
            y^=x;
            printf("%d\n",query(y));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/My_stage/article/details/77725403
今日推荐