Codeforces Round #430 (Div. 2) - D. Vitya and Strange Lesson (01字典树)

D. Vitya and Strange Lesson
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today at the lesson Vitya learned a very interesting function — mexMex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, mex([4, 33, 0, 1, 1, 5]) = 2 and mex([1, 2, 3]) = 0.

Vitya quickly understood all tasks of the teacher, but can you do the same?

You are given an array consisting of n non-negative integers, and m queries. Each query is characterized by one number x and consists of the following consecutive steps:

  • Perform the bitwise addition operation modulo 2 (xor) of each array element with the number x
  • Find mex of the resulting array. 

Note that after each query the array changes.

Input

First line contains two integer numbers n and m (1 ≤ n, m ≤ 3·105) — number of elements in array and number of queries.

Next line contains n integer numbers ai (0 ≤ ai ≤ 3·105) — elements of then array.

Each of next m lines contains query — one integer number x (0 ≤ x ≤ 3·105).

Output

For each query print the answer on a separate line.

Examples
input
2 2
1 3
1
3
output
1
0
input
4 3
0 1 5 6
1
2
4
output
2
0
0
input
5 4
0 1 5 6 7
1
1
4
5
output
2
2
0
2


题意:

给你n个数,每次去异或一个数,找出异或后不存在的最小自然数。

POINT:

01字典树处理,具体写在代码里。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
using namespace std;
#define LL long long
const int maxn = 2000000*3;
int tree[maxn][2];
int val[maxn];
int sz=0;
map<int,int>mp;
void build(int x)
{
    int u=0;
    for(int i=20;i>=0;i--)
    {
        int now=(x>>i)&1;
        if(tree[u][now]==0)
            tree[u][now]=++sz;
        u=tree[u][now];
        val[u]++;
    }
}
int query(int x)
{
    int ans=0;
    int u=0;
    for(int i=20;i>=0;i--)
    {
        int now=(x>>i)&1;
        if(tree[u][now]==0)//如果找不到了,那异或后的答案肯定比现在的ans大。
            return ans;
        if(val[tree[u][now]]==1<<i)//当前树下面有1<<i个数,即包括了所有可能性,说明ans肯定要+(1<<i)。
        {
            ans=ans|1<<i;
            u=tree[u][now^1];
        }
        else
            u=tree[u][now];
    }
    return ans;
}
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int a;scanf("%d",&a);
        if(mp[a]==0)
        {
            mp[a]=1,build(a);
        }
    }
    int now=0;
    for(int i=1;i<=m;i++)
    {
        int a;scanf("%d",&a);
        now=now^a;
        printf("%d\n",query(now));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值