Luogu P4462 [CQOI2018]异或序列

题目描述

https://www.luogu.com.cn/problem/P4462

Solution

注意到 $k$ 和 $a[i]$ 的范围都很小,可以直接用数组存

且 $s[r]\oplus s[l - 1] = k\rightarrow k\oplus s[r]=s[l-1]$

这个东西可以用莫队来转移

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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define maxn 100010
#define ll long long
using namespace std;

int n, m, k, a[maxn], s[maxn];

int blo;
struct Query {
int l, r, id;

friend bool operator < (const Query &u, const Query &v) {
if (u.l / blo == v.l / blo) return u.l / blo & 1 ? u.r < v.r : u.r > v.r;
return u.l < v.l;
}
} Q[maxn];

int cnt[1 << 17]; ll ans, Ans[maxn];
inline void add(int v) {
ans += cnt[k ^ v];
++cnt[v];
}

inline void del(int v) {
ans -= cnt[k ^ v];
--cnt[v];
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> m >> k; blo = sqrt(n);
for (int i = 1; i <= n; ++i) cin >> a[i], s[i] = s[i - 1] ^ a[i];
for (int i = 1; i <= m; ++i) cin >> Q[i].l >> Q[i].r, Q[i].id = i, --Q[i].l;
sort(Q + 1, Q + m + 1);
int l = Q[1].l, r = l - 1;
for (int i = 1; i <= m; ++i) {
while (r < Q[i].r) add(s[++r]);
while (l > Q[i].l) add(s[--l]);
while (r > Q[i].r) del(s[r--]);
while (l < Q[i].l) del(s[l++]);
Ans[Q[i].id] = ans;
}
for (int i = 1; i <= m; ++i) cout << Ans[i] << "\n";
return 0;
}