Guest User

Untitled

a guest
May 7th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 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. /****************************** CODE IS HERE ***********************************/
  12.  
  13. struct XOR_Trie{
  14.   XOR_Trie* ch[2];
  15.   bool isEnd;
  16.   XOR_Trie(){
  17.     ch[0] = ch[1] = nullptr;
  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[num] == nullptr){
  26.         curr->ch[num] = new XOR_Trie();
  27.       }
  28.       curr = curr->ch[num];
  29.     }
  30.     curr->isEnd = true;
  31.   }
  32.  
  33.   int MIN(int x){
  34.     XOR_Trie *curr = this;
  35.     int res = 0;
  36.     for (int i = 30; i >= 0; --i){
  37.       bool num = x & (1 << i);
  38.       if(curr->ch[num] != nullptr){
  39.         curr = curr->ch[num];
  40.       }
  41.       else{
  42.         res |= (1 << i);
  43.         curr = curr->ch[!num];
  44.       }
  45.     }
  46.     return res;
  47.   }
  48.  
  49.  
  50. };
  51.  
  52. int main(){
  53.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  54.  
  55.     XOR_Trie trie;
  56.     trie.insert(0);
  57.  
  58.     int q; cin >> q;
  59.     int init = 0;
  60.     while(q--){
  61.       int type; cin >> type;
  62.       if(type == 1){
  63.         int x; cin >> x;
  64.         trie.insert(x^init);
  65.  
  66.       }
  67.       else if(type == 2){
  68.         int x; cin >> x;
  69.         init ^= x;
  70.         // trie.Xor(x);
  71.       }
  72.       else{
  73.         cout << trie.MIN(init) << endl;
  74.       }
  75.     }
  76.  
  77.   return 0;
  78. }
  79.  
  80.  
  81. //https://www.hackerearth.com/practice/notes/lalitkundu95/tutorial-on-trie-and-example-problems/
Add Comment
Please, Sign In to add comment