Created
August 11, 2016 18:37
-
-
Save animeshf/5cae39bb3da10571d219acfa74e34eca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bits/stdc++.h" | |
using namespace std; | |
const int N = 200000 + 50; | |
const int LN = 32; | |
int cur, lc[N * LN], rc[N * LN], leaves[N * LN]; | |
inline void update(int node, int i, int val, int add){ | |
leaves[node] += add; | |
if(i == -1) return; | |
if(val & (1 << i)){ | |
if(rc[node] == -1){ | |
rc[node] = ++cur; | |
} | |
update(rc[node], i - 1, val, add); | |
} | |
else{ | |
if(lc[node] == -1){ | |
lc[node] = ++cur; | |
} | |
update(lc[node], i - 1, val, add); | |
} | |
} | |
inline int query(int node, int i, int val){ | |
if(i == -1) return 0; | |
if(val & (1 << i)){ | |
if((lc[node] != -1) and (leaves[lc[node]] > 0)) | |
return ((1 << i) | query(lc[node], i - 1, val)); | |
else if((rc[node] != -1) and (leaves[rc[node]] > 0)) | |
return (query(rc[node], i - 1, val)); | |
else return 0; | |
} | |
else{ | |
if((rc[node] != -1) and (leaves[rc[node]] > 0)) | |
return ((1 << i) | query(rc[node], i - 1, val)); | |
else if((lc[node] != -1) and (leaves[lc[node]] > 0)) | |
return (query(lc[node], i - 1, val)); | |
else return 0; | |
} | |
} | |
int main(){ | |
for(int i = 0; i < N * LN; i++){ | |
lc[i] = rc[i] = -1; | |
} | |
int q; scanf("%d", &q); | |
char cmd[1]; int x; | |
while(q--){ | |
scanf("%s %d", cmd, &x); | |
if(cmd[0] == '+'){ | |
update(0, 30, x, 1); | |
} | |
else if(cmd[0] == '-'){ | |
update(0, 30, x, -1); | |
} | |
else{ | |
printf("%d\n", max(x, query(0, 30, x))); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment