Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- tutorial : https://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems
- problem : http://codeforces.com/contest/842/problem/D
- */
- #include<bits/stdc++.h>
- using namespace std;
- #define si(n) scanf("%d",&n)
- struct Trie{
- Trie * nxt0;
- Trie * nxt1;
- int sz;
- Trie() : nxt0(NULL),nxt1(NULL),sz(0){}
- };
- typedef Trie * trie;
- const int MAX = 3e5+5;
- const int MAXLOG =20;
- int used[MAX];
- void add(trie root,int n)
- {
- root->sz ++;
- for(int i=MAXLOG-1;i>=0;i--){
- if(n & (1<<i)){
- if(root->nxt1 ==NULL)
- root->nxt1=new Trie();
- root=root->nxt1;
- }
- else{
- if(root->nxt0==NULL)
- root->nxt0=new Trie();
- root=root->nxt0;
- }
- root->sz++;
- }
- }
- int safe_sz(trie a){
- if(!a)return 0;
- return a->sz;
- }
- int mex(trie root)
- {
- int ans=0;
- for(int i=MAXLOG-1;i>=0;i--){
- if(root==NULL)return ans;
- if(root->nxt0==NULL && root->nxt1==NULL)
- return ans;
- if(used[i]%2==0){
- if(safe_sz(root->nxt0) != (1<<i))
- root=root->nxt0;
- else{
- ans+=(1<<i);
- root=root->nxt1;
- }
- }
- else{
- if (safe_sz(root -> nxt1) != (1 << i)) {
- root = root -> nxt1;
- }
- else {
- ans += (1 << i);
- root = root -> nxt0;
- }
- }
- }
- return ans;
- }
- int main()
- {
- int n,m,i,j,p;
- si(n);si(m);
- trie root=new Trie();
- map<int,int>gg;
- while(n--){
- si(p);
- if(++gg[p]==1){
- add(root,p);
- }
- }
- for(i=0;i<m;i++){
- si(p);
- for(j=MAXLOG-1;j>=0;j--){
- if(p & (1<<j))
- used[j]++;
- }
- printf("%d\n",mex(root));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement