Codeforces Round #430 (Div. 2)

来源:互联网 发布:软件测试课程大纲 编辑:程序博客网 时间:2024/06/16 10:04
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 21 313
output
10
input
4 30 1 5 6124
output
200
input
5 40 1 5 6 71145
output
2202


题意:

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

POINT:

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

#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>using namespace std;#define LL long longconst 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));    }}


原创粉丝点击