fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int MX = 3e6 + 5;
  12. const int N = 1e5 + 5;
  13.  
  14. struct Node {
  15. int nxt[2];
  16. int cnt = 0;
  17.  
  18. Node() {
  19. memset(nxt, 0, sizeof nxt);
  20. }
  21. };
  22.  
  23. int sz;
  24. Node trie[MX];
  25.  
  26. void addNumber(int x) {
  27. int v = 0;
  28. for (int i = 29; i >= 0; i--) {
  29. int c = (x >> i) & 1;
  30. if (trie[v].nxt[c] == 0) {
  31. trie[v].nxt[c] = ++sz;
  32. }
  33. v = trie[v].nxt[c];
  34. trie[v].cnt++;
  35. }
  36. }
  37.  
  38. // Tìm số lớn thứ k khi mỗi số x đều xor với sum_xor
  39. int findKth(int k, int sum_xor) {
  40. int v = 0, ans = 0;
  41. for (int i = 29; i >= 0; i--) {
  42. int c_sum = (sum_xor >> i) & 1;
  43. int nxt_v0 = trie[v].nxt[c_sum], nxt_v1 = trie[v].nxt[c_sum ^ 1];
  44.  
  45. if (k <= trie[nxt_v1].cnt) {
  46. ans |= (1 << i);
  47. v = nxt_v1;
  48. }
  49. else {
  50. k -= trie[nxt_v1].cnt;
  51. v = nxt_v0;
  52. }
  53. }
  54.  
  55. return ans;
  56. }
  57.  
  58. int n, q;
  59. int a[N];
  60.  
  61. int main() {
  62. ios::sync_with_stdio(false);
  63. cin.tie(nullptr);
  64. cin >> n >> q;
  65. for (int i = 1; i <= n; i++) cin >> a[i];
  66.  
  67. for (int i = 1; i <= n; i++) addNumber(a[i]);
  68.  
  69. int sum_xor = 0;
  70.  
  71. while (q--) {
  72. string type; cin >> type;
  73.  
  74. if (type == "XOR") {
  75. int x; cin >> x;
  76. sum_xor ^= x;
  77. }
  78. else {
  79. int k; cin >> k;
  80. cout << findKth(k, sum_xor) << '\n';
  81. }
  82. }
  83. }
Success #stdin #stdout 0.01s 38648KB
stdin
4 9
1 2 3 4
FIND 1
FIND 2
FIND 3
FIND 4
XOR 6
FIND 1
FIND 2
FIND 3
FIND 4
stdout
4
3
2
1
7
5
4
2