1 条题解
-
0
异或与最爱数字(XOR and Favorite Number) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; struct Query { int l, r, id, blk; bool operator<(const Query& o) const { if (blk != o.blk) return blk < o.blk; return (blk & 1) ? (r > o.r) : (r < o.r); } }; int cnt[1 << 21]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; if (!(cin >> n >> m >> k)) return 0; vector<int> pref(n + 1, 0); for (int i = 1; i <= n; ++i) { int a; cin >> a; pref[i] = pref[i - 1] ^ a; } int B = max(1, (int)(n / sqrt(max(1, m)))); vector<Query> qs(m); for (int i = 0; i < m; ++i) { int l, r; cin >> l >> r; qs[i] = {l - 1, r, i, (l - 1) / B}; } sort(qs.begin(), qs.end()); long long curAns = 0; auto add = [&](int x) { curAns += cnt[x ^ k]; cnt[x]++; }; auto del = [&](int x) { cnt[x]--; curAns -= cnt[x ^ k]; }; int curL = 0, curR = -1; vector<long long> ans(m); for (const auto& qu : qs) { while (curL > qu.l) add(pref[--curL]); while (curR < qu.r) add(pref[++curR]); while (curL < qu.l) del(pref[curL++]); while (curR > qu.r) del(pref[curR--]); ans[qu.id] = curAns; } for (int i = 0; i < m; ++i) cout << ans[i] << "\n"; return 0; }
- 1
信息
- ID
- 8660
- 时间
- 4000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者