Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using std::cerr;
  4. using std::endl;
  5.  
  6. inline int rd() {
  7. int a = 1, b = 0; char c = getchar();
  8. while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar();
  9. while (isdigit(c)) b = b * 10 + c - '0', c = getchar();
  10. return a ? b : -b;
  11. }
  12.  
  13. const int M = 1.7e7;
  14.  
  15. int ch[M][2], sum[M], tot;
  16.  
  17. inline void insert(int &root, int v) {
  18. if (!root) root = ++tot;
  19. int x = root;
  20. for (int i = 30; ~i; --i) {
  21. int t = (v >> i) & 1;
  22. if (!ch[x][t])
  23. ch[x][t] = ++tot;
  24. x = ch[x][t];
  25. ++sum[x];
  26. }
  27. }
  28.  
  29. int cnt, root;
  30.  
  31. std::map<int, int> rootX, rootY, rootZ;
  32.  
  33. struct Node {
  34. int x, y, z;
  35. };
  36.  
  37. inline int operator<(const Node &a, const Node &b) {
  38. if (a.x == b.x) {
  39. if (a.y == b.y)
  40. return a.z < b.z;
  41. return a.y < b.y;
  42. }
  43. return a.x < b.x;
  44. }
  45.  
  46. std::map<Node, int> rootXYZ;
  47.  
  48. const int N = 3e5 + 233;
  49. int n, opt, A[N], pre[N][3], X[N], Y[N], Z[N], lastans;
  50.  
  51. inline int query(int pos) {
  52.  
  53. int a = root, b = rootX[X[pos]], c = rootY[Y[pos]],
  54. d = rootZ[Z[pos]], e = rootXYZ[{ X[pos], Y[pos], Z[pos] }];
  55.  
  56. int ret = 0;
  57.  
  58. for (int i = 30; ~i; --i) {
  59. int t = ((A[pos] >> i) & 1) ^ 1;
  60. if (sum[ch[a][t]] - sum[ch[b][t]] - sum[ch[c][t]] - sum[ch[d][t]] + 2 * sum[ch[e][t]] > 0) {
  61. a = ch[a][t], b = ch[b][t], c = ch[c][t], d = ch[d][t], e = ch[e][t];
  62. ret |= 1 << i;
  63. } else {
  64. t ^= 1;
  65. a = ch[a][t], b = ch[b][t], c = ch[c][t], d = ch[d][t], e = ch[e][t];
  66. }
  67. }
  68.  
  69. insert(root, A[pos]);
  70. insert(rootX[X[pos]], A[pos]);
  71. insert(rootY[Y[pos]], A[pos]);
  72. insert(rootZ[Z[pos]], A[pos]);
  73. insert(rootXYZ[{ X[pos], Y[pos], Z[pos] }], A[pos]);
  74. return ret;
  75.  
  76. }
  77.  
  78. int main() {
  79. n = rd(), opt = rd();
  80. for (int i = 1; i <= n; ++i) {
  81. for (int j = 0; j < 3; ++j)
  82. pre[i][j] = pre[i - 1][j];
  83. ++pre[i][rd()];
  84. X[i] = pre[i][0] - pre[i][1];
  85. Y[i] = pre[i][1] - pre[i][2];
  86. Z[i] = pre[i][2] - pre[i][0];
  87. }
  88.  
  89. insert(root, 0);
  90. insert(rootX[0], 0);
  91. insert(rootY[0], 0);
  92. insert(rootZ[0], 0);
  93. insert(rootXYZ[{ 0, 0, 0 }], 0);
  94.  
  95. for (int i = 1; i <= n; ++i) {
  96. A[i] = rd();
  97. if (opt) A[i] ^= lastans;
  98. A[i] ^= A[i - 1];
  99. lastans = query(i);
  100. printf("%d ", lastans);
  101. }
  102. putchar('\n');
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement