CF842D Vitya and Strange Lesson

Title Link: Portal
Given a sequence, m operations give a number x at a time, let all the numbers in the sequence exclusive or upper x, and find the sequence mex after each operation

It's easy to meet 01 trie
Add the sequence to 01 trie after de duplication
Count the number under each subtree
When the left son is full, go right. The right son is the same
Go straight back to the empty node
Be sure to save a root node at the beginning
That is to say, cnt must be 1 at the beginning

/**
 * @Date:   2019-04-13T10:54:44+08:00
 * @Last modified time: 2019-04-13T14:16:27+08:00
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define A 3000010
#define B 2010

using namespace std;
typedef long long ll;
int siz[A], ch[A][2], n, m, a[A], b, s, cnt = 1;
void insert(int x, int fr = 1) {
    for (int i = 20; i > 0; i--) {
        siz[fr]++;
        if (x & (1 << (i - 1))) {
            if (!ch[fr][1]) ch[fr][1] = ++cnt;
            fr = ch[fr][1];
        }
        else {
            if (!ch[fr][0]) ch[fr][0] = ++cnt;
            fr = ch[fr][0];
        }
    }
    siz[fr]++;
}
int ask(int fr = 1, int ans = 0) {
    for (int i = 20; i > 0; i--)
        if (s & (1 << (i - 1))) {
            if (!ch[fr][1]) return ans;
            else if (siz[ch[fr][1]] < (1 << (i - 1))) fr = ch[fr][1];
            else fr = ch[fr][0], ans += (1 << (i - 1))
        }
        else {
            if (!ch[fr][0]) return ans;
            else if (siz[ch[fr][0]] < (1 << (i - 1))) fr = ch[fr][0];
            else fr = ch[fr][1], ans += (1 << (i - 1));
        }
    return ans;
}

int main(int argc, char const *argv[]) {
	cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
    for (int i = 1; i <= n; i++) insert(a[i]);
    while (m--) {
        cin >> b; s ^= b;
        cout << ask() << endl;
    }
    return 0;
}

Posted by aznkidzx on Thu, 28 Nov 2019 14:21:07 -0800