Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz size
- #define ll long long
- #define ld long double
- #define fs first
- #define sc second
- #define forn(i, f, t) for(int i = f; i < t; i++)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- const int INF = 2147483647;
- const int MOD = 1000000007;
- const ll INF64 = 9223372036854775807;
- const ld EPS = 1e-7;
- using namespace std;
- struct node{
- int next[2];
- int isFinal;
- } ar[1000001];
- int cnt = 0;
- void add(int x){
- int cur = 0;
- for (int i = 30; i >= 0; i--)
- if (ar[cur].next[x & (1 << i) ? 1 : 0] != -1){
- cur = ar[cur].next[x & (1 << i) ? 1 : 0];
- ar[cur].isFinal++;
- }
- else{
- ar[cur].next[x & (1 << i) ? 1 : 0] = ++cnt;
- cur = ar[cur].next[x & (1 << i) ? 1 : 0];
- ar[cur].isFinal = 1;
- ar[cur].next[0] = ar[cur].next[1] = -1;
- }
- }
- void rem(int x){
- int cur = 0;
- for (int i = 30; i >= 0; i--){
- cur = ar[cur].next[x & (1 << i) ? 1 : 0];
- ar[cur].isFinal--;
- }
- }
- int find(int x){
- int cur = 0, tmp = 0;
- for (int i = 30; i >= 0; i--)
- if (ar[cur].next[x & (1 << i) ? 0 : 1] != -1 && ar[ar[cur].next[x & (1 << i) ? 0 : 1]].isFinal){
- tmp += (1 << i);
- cur = ar[cur].next[x & (1 << i) ? 0 : 1];
- }
- else
- cur = ar[cur].next[x & (1 << i) ? 1 : 0];
- return tmp;
- }
- int main(){
- int n, x;
- scanf("%d", &n);
- char c;
- ar[0].next[0] = -1;
- ar[0].next[1] = -1;
- ar[0].isFinal = 0;
- forn(i, 0, n){
- scanf("\n%c %d", &c, &x);
- if (c == '+') add(x);
- else if (c == '-') rem(x);
- else if (c == '?') printf("%d\n", find(x));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement