地址:http://codeforces.com/contest/842/problem/D
题目:
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.
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).
For each query print the answer on a separate line.
2 2
1 3
1
3
1
0
4 3
0 1 5 6
1
2
4
2
0
0
5 4
0 1 5 6 7
1
1
4
5
2
2
0
2
思路:
很明显的trie树题,本来以为写完就ac了的,没想到一直wa6.。。。
今天起来后发现是把重复数字插入了,应该hash去重的。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int hs[1000000]; 5 struct Trie 6 { 7 int root, tot, next[1000005][2], cnt[1000005], end[1000005]; 8 9 inline int Newnode() 10 { 11 memset(next[tot], -1, sizeof(next[tot])); 12 cnt[tot] = 0; 13 end[tot] = 0; 14 return tot ++; 15 } 16 17 inline void Init() 18 { 19 tot = 0; 20 root = Newnode(); 21 } 22 23 inline int Insert(int x) 24 { 25 int p = root; 26 cnt[p] ++; 27 for(int i = 31; i >= 0; i --) 28 { 29 int idx = ((1 << i) & x) ? 1 : 0; 30 if(next[p][idx] == -1) 31 next[p][idx] = Newnode(); 32 p = next[p][idx]; 33 cnt[p] ++; 34 } 35 end[p] = x; 36 return 1; 37 } 38 inline int Search(int x) 39 { 40 int p = root; 41 int sum[50],ret=0; 42 sum[0]=1; 43 for(int i=1;i<31;i++) 44 sum[i]=sum[i-1]*2; 45 for(int i = 31; i >= 0; i --) 46 { 47 int idx = ((1 << i) & x) ? 1 : 0; 48 if(idx == 0) 49 { 50 if(next[p][0]==-1) return ret; 51 else if(cnt[next[p][0]]==sum[i]) 52 p = next[p][1],ret+=sum[i]; 53 else 54 p = next[p][0]; 55 } 56 else 57 { 58 if(next[p][1]==-1) return ret; 59 else if(cnt[next[p][1]]==sum[i]) 60 p = next[p][0],ret+=sum[i]; 61 else 62 p = next[p][1]; 63 } 64 if(p==-1) return ret; 65 } 66 return ret; 67 } 68 }tr; 69 70 int main(void) 71 { 72 int n,m; 73 cin>>n>>m; 74 tr.Init(); 75 for(int i=1,x;i<=n;i++) 76 scanf("%d",&x),hs[x]?0:hs[x]=tr.Insert(x); 77 for(int i=1,x,ls=0;i<=m;i++) 78 scanf("%d",&x),printf("%d ",tr.Search(ls=(x^ls))); 79 return 0; 80 }