Z res = 1; for (int i = 0; i < n; i++) { int L = 0; // 前缀 int sum = 0; for (int bit = 30; bit >= 0; bit--) { if (l[i] >> bit & 1) { // 这一位是 1 才能把 1 变成 0 if (((a[i] & L) == L)) { // 前缀要满足约束 sum += 1 << __builtin_popcount(a[i] & ((1 << bit) - 1)); // a[i] 后缀 1 的数量 } L |= 1 << bit; } } if ((a[i] & L) == L) { sum++; } res *= sum; } cout << res << endl;
usingnamespace std; using ll = longlong; using uint = unsigned; using ull = unsignedlonglong; using ulll = unsigned __int128;
constexpr ull B = 30;
structTrie { structUnit { int nxt[2]{}; int icnt{}; }; vector<Unit> U;
Trie() { new_node(); }
intnew_node(){ int p = U.size(); U.push_back({}); return p; }
voidinsert(uint x, int p = 1){ U[p].icnt++; for (int bit = B; bit >= 0; bit--) { bool c = x >> bit & 1; if (!U[p].nxt[c]) U[p].nxt[c] = new_node(); p = U[p].nxt[c]; U[p].icnt++; } }
// 字典树合并 返回新树 // int merge(int x, int y) { // if (!x || !y) return x | y; // int p = new_node(); // U[p].icnt = U[x].icnt + U[y].icnt; // for (int i = 0; i < 2; i++) { // U[p].nxt[i] = merge(U[x].nxt[i], U[y].nxt[i]); // } // return p; // }
// 字典树合并 直接合并到x上 intmerge(int x, int y){ if (!x || !y) return x | y; U[x].icnt += U[y].icnt; for (int i = 0; i < 2; i++) { U[x].nxt[i] = merge(U[x].nxt[i], U[y].nxt[i]); } return x; }
uint query(int l, int r, uint x){ uint max = 0; for (int bit = B; bit >= 0; bit--) { int c = x >> bit & 1 ^ 1; if (U[U[r].nxt[c]].icnt - U[U[l].nxt[c]].icnt > 0) { max |= 1u << bit; l = U[l].nxt[c], r = U[r].nxt[c]; } else { l = U[l].nxt[!c], r = U[r].nxt[!c]; } } return max; } };
intmain(){ int J; cin >> J; while (J--) { int n, q; cin >> n >> q; vector<int> pre(n + 1); Trie trie; pre[0] = trie.new_node(); for (int i = 0; i < n; i++) { int x; cin >> x; int rt = trie.new_node(); trie.insert(x, rt); pre[i + 1] = trie.merge(rt, pre[i]); } while (q--) { int l, r, x; cin >> l >> r >> x; l--; cout << (trie.query(pre[l], pre[r], x)) << endl; } } return0; }
intnew_node(){ int p = U.size(); U.push_back({}); return p; }
voidinsert(uint x, int id, int p = 0){ for (int bit = B; bit >= 0; bit--) { bool c = x >> bit & 1; if (!U[p].nxt[c]) U[p].nxt[c] = new_node(); p = U[p].nxt[c]; U[p].vec.push_back(id); } }
uint query(uint x, int l, int r, int p = 0){ uint res = 0; for (int bit = B; bit >= 0; bit--) { bool c = x >> bit & 1 ^ 1; if (U[p].nxt[c]) { auto& vec = U[U[p].nxt[c]].vec; auto it = lower_bound(vec.begin(), vec.end(), l); if (it != vec.end() && *it >= l && *it <= r) { res |= 1u << bit; p = U[p].nxt[c]; } else { p = U[p].nxt[!c]; } } else { p = U[p].nxt[!c]; } } return res; } };
intmain(){ int J; cin >> J; while (J--) { int n, q; cin >> n >> q; Trie trie; for (int i = 0; i < n; i++) { int x; cin >> x; trie.insert(x, i); } while (q--) { int l, r, x; cin >> l >> r >> x; l--; r--; cout << trie.query(x, l, r) << endl; } } return0; }