Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long getMaxXor(long long);
- void addNewNum(long long);
- void delOldNum(long long);
- struct Trie;
- Trie *newNode();
- struct Trie{
- int cnt;
- Trie *next[2];
- }*root = newNode();
- Trie *newNode(){
- Trie *node;
- node = new Trie;
- node -> cnt = 0;
- node -> next[0] = node -> next[1] = NULL;
- return node;
- }
- long long getMaxXor(long long x){
- int Max = 0;
- Trie *Cur = root;
- for(int i=31 ; i>=0 ; i--){
- int T = x & (1 << i);
- bool idx = T ? 0 : 1;
- if(Cur -> next[idx] == NULL){
- if(Cur -> next[!idx] != NULL)
- idx = !idx;
- else if(Cur -> next[!idx] == NULL)
- return 0;
- }
- Cur = Cur -> next[idx];
- if(idx)
- Max |= ( 1 << i );
- }
- return Max;
- }
- void addNewNum(long long x){
- Trie *Cur = root;
- for(int i=31 ; i>=0 ; i--){
- int T = x & (1 << i);
- bool idx = T ? 1 : 0;
- if(Cur -> next[idx] == NULL){
- Cur -> next[idx] = newNode();
- }
- Cur = Cur -> next[idx];
- Cur -> cnt++;
- //cout << "new " << idx << ' ' << Cur -> cnt << ' ' << i << endl;
- }
- }
- void delOldNum(long long x){
- Trie *Cur = root;
- for(int i=31 ; i>=0 ; i--){
- int T = x & (1 << i);
- bool idx = T ? 1 : 0;
- Cur -> next[idx] -> cnt --;
- if(Cur -> next[idx] -> cnt == 0){
- Cur -> next[idx] = NULL;
- break;
- }else{
- Cur = Cur -> next[idx];
- }
- }
- }
- int main()
- {
- int Q; cin >> Q;
- while(Q--){
- char ch;
- long long x;
- cin >> ch >> x;
- switch(ch){
- case '+':{
- addNewNum(x);
- }break;
- case '-':{
- delOldNum(x);
- }break;
- case '?':{
- long long Max = getMaxXor(x);
- //cout << x << " asd " << Max << endl;
- cout << (Max^x) << endl;
- }break;
- }
- }
- return 0;
- }
- /*
- 4
- ? 1
- + 1
- + 4
- ? 5
- */
Advertisement
Add Comment
Please, Sign In to add comment