Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using std::cerr;
- using std::endl;
- inline int rd() {
- int a = 1, b = 0; char c = getchar();
- while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar();
- while (isdigit(c)) b = b * 10 + c - '0', c = getchar();
- return a ? b : -b;
- }
- const int M = 1.7e7;
- int ch[M][2], sum[M], tot;
- inline void insert(int &root, int v) {
- if (!root) root = ++tot;
- int x = root;
- for (int i = 30; ~i; --i) {
- int t = (v >> i) & 1;
- if (!ch[x][t])
- ch[x][t] = ++tot;
- x = ch[x][t];
- ++sum[x];
- }
- }
- int cnt, root;
- std::map<int, int> rootX, rootY, rootZ;
- struct Node {
- int x, y, z;
- };
- inline int operator<(const Node &a, const Node &b) {
- if (a.x == b.x) {
- if (a.y == b.y)
- return a.z < b.z;
- return a.y < b.y;
- }
- return a.x < b.x;
- }
- std::map<Node, int> rootXYZ;
- const int N = 3e5 + 233;
- int n, opt, A[N], pre[N][3], X[N], Y[N], Z[N], lastans;
- inline int query(int pos) {
- int a = root, b = rootX[X[pos]], c = rootY[Y[pos]],
- d = rootZ[Z[pos]], e = rootXYZ[{ X[pos], Y[pos], Z[pos] }];
- int ret = 0;
- for (int i = 30; ~i; --i) {
- int t = ((A[pos] >> i) & 1) ^ 1;
- if (sum[ch[a][t]] - sum[ch[b][t]] - sum[ch[c][t]] - sum[ch[d][t]] + 2 * sum[ch[e][t]] > 0) {
- a = ch[a][t], b = ch[b][t], c = ch[c][t], d = ch[d][t], e = ch[e][t];
- ret |= 1 << i;
- } else {
- t ^= 1;
- a = ch[a][t], b = ch[b][t], c = ch[c][t], d = ch[d][t], e = ch[e][t];
- }
- }
- insert(root, A[pos]);
- insert(rootX[X[pos]], A[pos]);
- insert(rootY[Y[pos]], A[pos]);
- insert(rootZ[Z[pos]], A[pos]);
- insert(rootXYZ[{ X[pos], Y[pos], Z[pos] }], A[pos]);
- return ret;
- }
- int main() {
- n = rd(), opt = rd();
- for (int i = 1; i <= n; ++i) {
- for (int j = 0; j < 3; ++j)
- pre[i][j] = pre[i - 1][j];
- ++pre[i][rd()];
- X[i] = pre[i][0] - pre[i][1];
- Y[i] = pre[i][1] - pre[i][2];
- Z[i] = pre[i][2] - pre[i][0];
- }
- insert(root, 0);
- insert(rootX[0], 0);
- insert(rootY[0], 0);
- insert(rootZ[0], 0);
- insert(rootXYZ[{ 0, 0, 0 }], 0);
- for (int i = 1; i <= n; ++i) {
- A[i] = rd();
- if (opt) A[i] ^= lastans;
- A[i] ^= A[i - 1];
- lastans = query(i);
- printf("%d ", lastans);
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement