Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define endl '\n'
- #define sz(v) (int)v.size()
- #define all(v) v.begin(), v.end()
- void dbg_out() { cerr << "\b\b]\n"; }
- template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
- #define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
- /****************************** CODE IS HERE ***********************************/
- struct XOR_Trie{
- XOR_Trie* ch[2];
- bool isEnd;
- XOR_Trie(){
- ch[0] = ch[1] = nullptr;
- this->isEnd = false;
- }
- void insert(int x){
- XOR_Trie *curr = this;
- for (int i = 30; i >= 0; --i){
- bool num = x & (1 << i);
- if(curr->ch[num] == nullptr){
- curr->ch[num] = new XOR_Trie();
- }
- curr = curr->ch[num];
- }
- curr->isEnd = true;
- }
- int MIN(int x){
- XOR_Trie *curr = this;
- int res = 0;
- for (int i = 30; i >= 0; --i){
- bool num = x & (1 << i);
- if(curr->ch[num] != nullptr){
- curr = curr->ch[num];
- }
- else{
- res |= (1 << i);
- curr = curr->ch[!num];
- }
- }
- return res;
- }
- };
- int main(){
- ios_base::sync_with_stdio(false); cin.tie(nullptr);
- XOR_Trie trie;
- trie.insert(0);
- int q; cin >> q;
- int init = 0;
- while(q--){
- int type; cin >> type;
- if(type == 1){
- int x; cin >> x;
- trie.insert(x^init);
- }
- else if(type == 2){
- int x; cin >> x;
- init ^= x;
- // trie.Xor(x);
- }
- else{
- cout << trie.MIN(init) << endl;
- }
- }
- return 0;
- }
- //https://www.hackerearth.com/practice/notes/lalitkundu95/tutorial-on-trie-and-example-problems/
Add Comment
Please, Sign In to add comment