706D - Vasiliy's Multiset - CodeForces Solution


binary search bitmasks data structures trees *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(x...) 42
#endif
const int N = 2e5 + 5;
int a[N * 31], timer = 0, bor[N * 31][2], mx = 31;


void add(int x) {
    int root = 0;
    a[root]++;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        if(bor[root][bit] == 0) bor[root][bit] = ++timer;
        root = bor[root][bit];
        a[root]++;
    }
}

void del(int x) {
    int root = 0;
    a[root]--;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        root = bor[root][bit];
        a[root]--;
    }
}

int go(int x) {
    int root = 0, res = 0;
    for(int i = mx - 1; i >= 0; i--) {
        int bit = ((x >> i) & 1);
        int need = (bit ^ 1);
        if(bor[root][need] && a[bor[root][need]]) {
            root = bor[root][need];
            res |= (1LL << i);
        } else root = bor[root][bit];
    }
    return res;
}

void solve() {
    int n, x;
    cin >> n;
    char c;
    add(0);
    for(int i = 1; i <= n; i++) {
        cin >> c >> x;
        if(c == '+') {
            add(x);
        } else if (c == '-') {
            del(x);
        } else {
            cout << go(x) << "\n";
        }
    }
}


int main() {
    ios_base::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int t = 1;
    while(t--) solve();
    return 0;
}
 	 		   	 	 	  			 	  	    				


Comments

Submit
0 Comments
More Questions

617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check