Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define debug(l) cerr<<#l<<' '<<l<<'\n';
- #include "bits/stdc++.h"
- using namespace std;
- #define all(a) a.begin(), a.end()
- typedef long long ll;
- typedef long double ld;
- int cnt[62][2] = {};
- struct node {
- node* go[2];
- int val = 0, cnt = 0;
- node() {
- go[0] = nullptr;
- go[1] = nullptr;
- }
- };
- int max_bit(long long x) {
- int mx = 0;
- for (int i = 0; i <= 60; i++) {
- if ((x >> i) & (1ll)) {
- mx = i;
- }
- }
- return mx;
- }
- void add(long long x, node& Trie) {
- node* cur = &Trie;
- cur->val++;
- int bit = max_bit(x);
- for (ll i = 0; i <= 60; i++) {
- int k = (x >> i) & (1ll);
- cnt[i][k]++;
- if (cur->go[k] == nullptr) {
- cur->go[k] = new node();
- cur = cur->go[k];
- cur->val++;
- }
- else {
- cur = cur->go[k];
- cur->val++;
- }
- if (i == bit) {
- cur->cnt++;
- }
- }
- }
- void push(node& Trie, int bit) {
- node* cur = &Trie;
- if (cur == nullptr) {
- return;
- }
- cnt[bit][0] -= (cur->go[0] == nullptr ? (int)0 : cur->go[0]->val);
- cnt[bit][1] += (cur->go[0] == nullptr ? (int)0 : cur->go[0]->val);
- cnt[bit][0] += (cur->go[1] == nullptr ? (int)0 : cur->go[1]->val);
- cnt[bit][1] -= (cur->go[1] == nullptr ? (int)0 : cur->go[1]->val);
- swap(cur->go[1], cur->go[0]);
- if (cur->cnt > 0) {
- int tmp = cur->cnt;
- cur->cnt = 0;
- if (cur->go[1] == nullptr) {
- cur->go[1] = new node();
- }
- cur->go[1]->cnt += tmp;
- }
- if (cur->go[0] != nullptr && cur->go[0]->val > 0) {
- push(*(cur->go[0]), bit + 1);
- }
- }
- bool find(ll x, node& trie) {
- node* cur = ≜
- int to = max_bit(x);
- for (int i = 0; i <= to; i++) {
- int k = (x >> i) & (1ll);
- if (cur->go[k] == nullptr || cur->go[k] == 0) {
- return false;
- }
- cur = cur->go[k];
- }
- return cur->cnt ? cur->cnt--, true : false;
- }
- void erase(ll x, node& trie) {
- if (!find(x, trie))return;
- node* cur = ≜
- cur->val--;
- for (ll i = 0; i <= 60; i++) {
- ll k = (x >> i) & (1ll);
- cnt[i][k]--;
- cur = cur->go[k];
- cur->val--;
- }
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- srand(time(NULL));
- int n, q;
- cin >> n >> q;
- node Trie;
- for (int i = 0; i < n; i++) {
- ll x;
- cin >> x;
- add(x, Trie);
- }
- auto get_ans = [&]() {
- ll ans = 0;
- for (ll i = 0; i <= 60; i++) {
- if (cnt[i][1] & 1) {
- ans += (1ll << i);
- }
- }
- return ans;
- };
- while (q--) {
- ll type;
- cin >> type;
- if (type == 1) {
- push(Trie, 0);
- }
- else if (type == 2) {
- ll x;
- cin >> x;
- add(x, Trie);
- }
- else {
- ll x;
- cin >> x;
- erase(x, Trie);
- }
- cout << get_ans() << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement