Advertisement
ivnikkk

Untitled

Feb 13th, 2023
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. typedef long long ll;
  7. typedef long double ld;
  8. int cnt[62][2] = {};
  9. struct node {
  10.     node* go[2];
  11.     int val = 0, cnt = 0;
  12.     node() {
  13.         go[0] = nullptr;
  14.         go[1] = nullptr;
  15.     }
  16. };
  17.  
  18. int max_bit(long long x) {
  19.     int mx = 0;
  20.     for (int i = 0; i <= 60; i++) {
  21.         if ((x >> i) & (1ll)) {
  22.             mx = i;
  23.         }
  24.     }
  25.     return mx;
  26. }
  27. void add(long long x, node& Trie) {
  28.     node* cur = &Trie;
  29.     cur->val++;
  30.     int bit = max_bit(x);
  31.     for (ll i = 0; i <= 60; i++) {
  32.         int k = (x >> i) & (1ll);
  33.         cnt[i][k]++;
  34.         if (cur->go[k] == nullptr) {
  35.             cur->go[k] = new node();
  36.             cur = cur->go[k];
  37.             cur->val++;
  38.         }
  39.         else {
  40.             cur = cur->go[k];
  41.             cur->val++;
  42.         }
  43.         if (i == bit) {
  44.             cur->cnt++;
  45.         }
  46.     }
  47. }
  48. void push(node& Trie, int bit) {
  49.     node* cur = &Trie;
  50.     if (cur == nullptr) {
  51.         return;
  52.     }
  53.     cnt[bit][0] -= (cur->go[0] == nullptr ? (int)0 : cur->go[0]->val);
  54.     cnt[bit][1] += (cur->go[0] == nullptr ? (int)0 : cur->go[0]->val);
  55.     cnt[bit][0] += (cur->go[1] == nullptr ? (int)0 : cur->go[1]->val);
  56.     cnt[bit][1] -= (cur->go[1] == nullptr ? (int)0 : cur->go[1]->val);
  57.     swap(cur->go[1], cur->go[0]);
  58.     if (cur->cnt > 0) {
  59.         int tmp = cur->cnt;
  60.         cur->cnt = 0;
  61.         if (cur->go[1] == nullptr) {
  62.             cur->go[1] = new node();
  63.         }
  64.         cur->go[1]->cnt += tmp;
  65.     }
  66.     if (cur->go[0] != nullptr && cur->go[0]->val > 0) {
  67.         push(*(cur->go[0]), bit + 1);
  68.     }
  69. }
  70. bool find(ll x, node& trie) {
  71.     node* cur = &trie;
  72.     int to = max_bit(x);
  73.     for (int i = 0; i <= to; i++) {
  74.         int k = (x >> i) & (1ll);
  75.         if (cur->go[k] == nullptr || cur->go[k] == 0) {
  76.             return false;
  77.         }
  78.         cur = cur->go[k];
  79.     }
  80.     return cur->cnt ? cur->cnt--, true : false;
  81. }
  82. void erase(ll x, node& trie) {
  83.     if (!find(x, trie))return;
  84.     node* cur = &trie;
  85.     cur->val--;
  86.     for (ll i = 0; i <= 60; i++) {
  87.         ll k = (x >> i) & (1ll);
  88.         cnt[i][k]--;
  89.         cur = cur->go[k];
  90.         cur->val--;
  91.     }
  92. }
  93. signed main() {
  94. #ifdef _DEBUG
  95.     freopen("input.txt", "r", stdin);
  96.     freopen("output.txt", "w", stdout);
  97. #endif
  98.     ios_base::sync_with_stdio(false);
  99.     cin.tie(nullptr);
  100.     cout.tie(nullptr);
  101.     srand(time(NULL));
  102.     int n, q;
  103.     cin >> n >> q;
  104.     node Trie;
  105.     for (int i = 0; i < n; i++) {
  106.         ll x;
  107.         cin >> x;
  108.         add(x, Trie);
  109.     }
  110.     auto get_ans = [&]() {
  111.         ll ans = 0;
  112.         for (ll i = 0; i <= 60; i++) {
  113.             if (cnt[i][1] & 1) {
  114.                 ans += (1ll << i);
  115.             }
  116.         }
  117.         return ans;
  118.     };
  119.     while (q--) {
  120.         ll type;
  121.         cin >> type;
  122.         if (type == 1) {
  123.             push(Trie, 0);
  124.         }
  125.         else if (type == 2) {
  126.             ll x;
  127.             cin >> x;
  128.             add(x, Trie);
  129.         }
  130.         else {
  131.             ll x;
  132.             cin >> x;
  133.             erase(x, Trie);
  134.         }
  135.         cout << get_ans() << '\n';
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement