您的位置:首页 > 其它

Codeforces 842D Vitya and Strange Lesson【逆向思维+字典树查询亦或最小值】

2017-08-30 16:45 260 查看
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 — mex. Mex 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


题目大意:

给出一个数组,每次操作将整个数组亦或一个数x,问得到的数组的结果中的mex.mex表示为自然数中第一个没有出现过的数。

思路:

①首先我们知道,亦或x之后得到的数组将其再亦或y值得的数组,其实就是原数组亦或(x^y)的结果。所以这里我们没有必要将原数组进行变化。

②我们还知道,如果有:z^x=y,其中若x,y已知,那么z是固定的。那么我们可以根据这个特性,我们将问题翻转一下。

我们将原数组中不存在的数入树。那么对于每一个查询temp,其实就相当于我们在树上找一个值,使得这个值亦或temp最小。

引入了这个逆向思维之后,我们就变成了贪心find。

③过程维护一下即可。注意入树的数值范围。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define ll long long int
#define maxn 2
typedef struct tree
{
tree *nex[maxn];
ll v;
ll val;
} tree;
tree root;
void init()
{
for(ll i=0; i<maxn; i++)
{
root.nex[i]=NULL;
root.v=0;
root.val=0;
}
}
void creat(char *str,int va)
{
int len=strlen(str);
tree *p=&root,*q;
for(int i=0; i<len; i++)
{
int id=str[i]-'0';
if(p->nex[id]==NULL)
{
q=(tree *)malloc(sizeof(root));
q->v=1;
for(int j=0; j<2; j++)
{
q->nex[j]=NULL;
}
p->nex[id]=q;
}
else
{
p->nex[id]->v++;
}
p=p->nex[id];
if(i==len-1)
{
p->val=va;
}
}
}
void find(char *str,ll query)
{
ll len=strlen(str);
tree *p=&root;
for(ll i=0; i<len; i++)
{
ll id=str[i]-'0';
if(p->nex[id]!=NULL)
{
p=p->nex[id];
}
else p=p->nex[1-id];
if(p==NULL)return ;
}
printf("%lld\n",p->val^query);
}
int main()
{
ll n,q;
while(~scanf("%lld%lld",&n,&q))
{
init();
map<ll,ll>s;
for(ll i=1; i<=n; i++)
{
ll x;
scanf("%lld",&x);
if(s[x]==0)
{
s[x]=1;
}
}
for(ll i=0; i<=600000; i++)
{
if(s[i]==0)
{
char ss[25];
ll x=i;
ss[21]='\0';
for(int j=20; j>=0; j--)
{
if(x)
{
ss[j]=x%2+'0';
x/=2;
}
else
{
ss[j]='0';
}
}
creat(ss,i);
}
}
ll temp=0;
while(q--)
{
ll x;
scanf("%lld",&x);
temp^=x;
x=temp;
char ss[25];
ss[21]='\0';
for(int j=20; j>=0; j--)
{
if(x)
{
ss[j]=x%2+'0';
x/=2;
}
else
{
ss[j]='0';
}
}
find(ss,temp);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 842D