Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int MAX = 2e5 * 32;
- vector<node> t(MAX);
- int leaf = 1;
- string bin(int &num) {
- string res;
- while (num) {
- res += '0' + (num & 1);
- num >>= 1;
- }
- reverse(all(res));
- int k = 30 - res.size();
- string zeroes;
- for (int i = 0; i < k; ++i) zeroes += '0';
- return zeroes + res;
- }
- int unbin(string &num) {
- int res = 0; int deg = (1 << (int)(num.size()) - 1);
- for (auto el : num) {
- int k = el - '0';
- res += k * deg;
- deg >>= 1;
- }
- return res;
- }
- void add(int &num) {
- int v = 0;
- string bin_num = bin(num);
- for (auto el : bin_num) {
- int k = el - '0';
- if (t[v].next[k] == 0) {
- t[v].next[k] = leaf++;
- }
- v = t[v].next[k]; t[v].cnt++;
- }
- }
- void del(int &num) {
- int v = 0;
- string bin_num = bin(num);
- for (auto el : bin_num) {
- v = t[v].next[el - '0'];
- t[v].cnt--;
- }
- }
- void get(int &num) {
- int v = 0;
- int num1 = num;
- string bin_num = bin(num1);
- string ans;
- for (auto el : bin_num) {
- int k = el - '0';
- if (t[t[v].next[1 - k]].cnt) {
- v = t[v].next[1 - k];
- ans += (1 - k) + '0';
- } else {
- v = t[v].next[k];
- ans += el;
- }
- }
- cout << (unbin(ans) ^ num) << '\n';
- }
- int main() {
- int q;
- cin >> q;
- char type; int x;
- while (q--) {
- cin >> type >> x;
- if (type == '+') {
- add(x);
- } else if (type == '-') {
- del(x);
- } else {
- get(x);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement