1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| struct LinearBasis { ll d[64]; ll p[64]; int cnt; bool has_zero; ll ps[64]; LinearBasis() { fill(d, d + 64, 0); fill(p, p + 64, 0); fill(ps, ps + 64, 0); cnt = 0; has_zero = false; }
bool insert(ll x, int pos) { for (int i = 62; i >= 0; i--) { if (!x) break; if (!(x >> i)) continue; if (!d[i]) { d[i] = x; cnt++; ps[i] = pos; return true; } if (pos > ps[i]) { swap(x, d[i]); swap(pos, ps[i]); } x ^= d[i]; } has_zero = true; return false; } };
struct qury { int l, r; int k; int id; }; void solve() { int n, q; cin >> n >> q; vi nums(n); for (int i = 0; i < n; i++) { cin >> nums[i]; } vector<qury> qry; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--, r--; int k; cin >> k; qry.push_back({l, r, k, i}); } LinearBasis lb; sort(all(qry), [](qury a, qury b) { if(a.r!=b.r)return a.r<b.r; else return a.l>b.l; }); int tp = 0; vi ans(q); for (int i = 0; i < q; i++) { auto [l, r, k, id] = qry[i]; while (tp <= r) { lb.insert(nums[tp], tp); tp++; } vi now(64); int c = 0; vi heit(64); for (int j = 60; j >= 0; j--) { if (lb.ps[j] >= l && lb.d[j]) { now[c] = lb.d[j]; heit[c] = j; c++; } }
if (k > (1ll << c)) { ans[id] = -1; } else { int res = 0; int rnk = (1ll << c) - k; for (int j = 0; j < c; j++) { int sod = (rnk >> (c - (j + 1))) & 1; int real = (res >> heit[j]) & 1;
if (sod ^ real) { res ^= now[j]; } } ans[id] = res; } } for (int i = 0; i < q; i++) { cout << ans[i] << '\n'; } }
|