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.
InputFirst 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).
OutputFor 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)); } }