Trie() { int sz = 1; // 注意这行后面加个括号,表示进行初始化,如果不加我也没碰到问题,但是加上比较保险。 for (int i = G-1; i >= 0; i--, sz <<= 6) A[i] = new ull[sz](); }
voidins(int x){ for (int i = 0; i < G; i++, x >>= 6) { ull mask = 1ull << (x & 63); if (A[i][x >> 6] & mask) return; A[i][x >> 6] |= mask; } }
voiddel(int x){ for (int i = 0; i < G; i++, x >>= 6) { if (A[i][x >> 6] &= ~(1ull << (x & 63))) return; } }
intpre(int x){ int i, res; for (i = 0; i < G; i++, x >>= 6) { ull s = A[i][x >> 6] & ((1ull << (x & 63)) - 1); if (!s) continue; res = x & ~63 | lg2highbit(s); break; } if (i >= G) return INVALID; for (; i; i--) res = res << 6 | lg2highbit(A[i-1][res]); return res; }
intsuc(int x){ int i, res; for (i = 0; i < G; i++, x >>= 6) { ull s = A[i][x >> 6] & (~((1ull << (x & 63)) - 1) << 1); if (!s) continue; res = x & ~63 | lg2lowbit(s); break; } if (i >= G) return INVALID; for (; i; i--) res = res << 6 | lg2lowbit(A[i-1][res]); return res; }
~Trie() { for (int i = 0; i < G; i++) delete[] A[i]; } };
intpre(int x){ int i, res; for (i = 0; i < G; i++, x >>= 6) { ull s = A[i][x >> 6] & ((1ull << (x & 63)) - 1); if (!s) continue; res = x & ~63 | lghb(s); break; } if (i >= G) return -INF; for (; i; i--) res = res << 6 | lghb(A[i-1][res]); return res; }
intsuc(int x){ int i, res; for (i = 0; i < G; i++, x >>= 6) { ull s = A[i][x >> 6] & ~((1ull << (x & 63)) - 1) << 1; if (!s) continue; res = x & ~63 | lglb(s); break; } if (i >= G) return INF; for (; i; i--) res = res << 6 | lglb(A[i-1][res]); return res; }
~Trie() { for (int i = 0; i < G; i++) delete[] A[i]; } } trie;
intmain(){ int n = read(), res = 0; for (int i = 1; i <= n; i++) { int a = read() + OFFSET; if (i == 1) res += a - OFFSET; elseif (!trie.has(a)) res += min(a - trie.pre(a), trie.suc(a) - a); trie.ins(a); } return !printf("%d\n", res); }
constint N = 210000, M = 530000; int tr[M][2], s[N], n, idx;
voidinsert(int a){ int p = 0; for (int i = 18; i >= 0; i--) { int b = a >> i & 1; if (!tr[p][b]) tr[p][b] = ++idx; p = tr[p][b]; } }
intget(int k){ int p = 0, res = 0; for (int i = 18; i >= 0; i--) { int b = k >> i & 1; if (tr[p][b^1]) res |= 1 << i, p = tr[p][b^1]; else p = tr[p][b]; } return res; }
intmain(){ scanf("%d", &n);
for (int i = 1; i < n; i++) { scanf("%d", &s[i]); s[i] ^= s[i-1]; insert(s[i]); }
for (int b1 = 0; b1 < n; b1++) { if (get(b1) < n) { printf("%d ", b1); for (int i = 1; i < n; i++) printf("%d ", b1 ^ s[i]); break; } }
return0; }
[CF665E] Beautiful Subarrays
给定 n 个元素的数组 a 以及 k,问多少个 [l,r] 满足 ⨁i=lrai≥k,1≤n≤106,1≤k,ai≤109。
constint N = 1000010, M = 30000010; int n, k, s[N], tr[M][2], sz[M], idx = 1;
voidins(int a, int p, int i = 30){ sz[p]++; if (i == -1) return; int b = a >> i & 1; if (!tr[p][b]) tr[p][b] = ++idx; ins(a, tr[p][b], i-1); }
intquery(int a, int p, int i = 30){ if (i == -1) return sz[p]; int b = a >> i & 1, t = k >> i & 1, res = 0; if (t) returnquery(a, tr[p][b^1], i-1); return sz[tr[p][b^1]] + query(a, tr[p][b], i-1); }
intmain(){ read(n, k); ins(0, 1);
longlong res = 0; for (int i = 1; i <= n; i++) { read(s[i]), s[i] ^= s[i-1]; res += query(s[i], 1); ins(s[i], 1); } returnwrite(res), 0; }