Advertisement
ivnikkk

Untitled

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