Guest User

Untitled

a guest
May 7th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define endl '\n'
  5. #define sz(v) (int)v.size()
  6. #define all(v) v.begin(), v.end()
  7. void dbg_out() { cerr << "\b\b]\n"; }
  8. template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
  9. #define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
  10.  
  11.  
  12. /****************************** CODE IS HERE ***********************************/
  13.  
  14. struct XOR_Trie{
  15.   unordered_map<bool, XOR_Trie*> ch;
  16.   bool isEnd;
  17.   XOR_Trie(){
  18.     this->isEnd = false;
  19.   }
  20.  
  21.   void insert(int x){
  22.     XOR_Trie *curr = this;
  23.     for (int i = 30; i >= 0; --i){
  24.       bool num = x & (1 << i);
  25.       if(!curr->ch.count(num))
  26.         curr->ch[num] = new XOR_Trie();
  27.       curr = curr->ch[num];
  28.     }
  29.     curr->isEnd = true;
  30.   }
  31.  
  32.  
  33.  
  34.   int MIN(int x){
  35.     XOR_Trie *curr = this;
  36.     int res = 0;
  37.     for (int i = 30; i >= 0; --i){
  38.       bool num = x & (1 << i);
  39.       if(curr->ch.count(num)){
  40.         curr = curr->ch[num];
  41.       }
  42.       else{
  43.         res |= (1 << i);
  44.         curr = curr->ch[!num];
  45.       }
  46.     }
  47.     return res;
  48.   }
  49.  
  50.  
  51. };
  52.  
  53. int main(){
  54.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  55.    
  56.     XOR_Trie trie;
  57.     trie.insert(0);
  58.  
  59.     int q; cin >> q;
  60.     int init = 0;
  61.     while(q--){
  62.       int type; cin >> type;
  63.       if(type == 1){
  64.         int x; cin >> x;
  65.         trie.insert(x^init);
  66.  
  67.       }
  68.       else if(type == 2){
  69.         int x; cin >> x;
  70.         init ^= x;
  71.         // trie.Xor(x);
  72.       }
  73.       else{
  74.         cout << trie.MIN(init) << endl;
  75.       }
  76.     }
  77.  
  78.   return 0;
  79. }
Add Comment
Please, Sign In to add comment