洛谷 P4462 [CQOI2018]异或序列
题意
给一个长度为 n n n 的序列,求在区间 [ l , r ] [l,r] [l,r] 内,有多少子序列满足异或和等于 k k k 。
解法
- 莫队
- 记录异或前缀和 p r e [ i ] pre[i] pre[i] ,那么子序列 a l , a l + 1 , … , a r a_l,a_{l+1},\dots,a_r al,al+1,…,ar 的异或和可以表示为 p r e [ r ] ⊕ p r e [ l − 1 ] pre[r]\oplus pre[l-1] pre[r]⊕pre[l−1] ,满足异或和等于 k k k ,也就是 p r e [ r ] ⊕ p r e [ l − 1 ] = k pre[r]\oplus pre[l-1]=k pre[r]⊕pre[l−1]=k ,即 p r e [ r ] = k ⊕ p r e [ l − 1 ] pre[r]=k\oplus pre[l-1] pre[r]=k⊕pre[l−1] ,使用 c n t cnt cnt 数组记录即可 O ( 1 ) O(1) O(1) 转移答案。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (sign)
x = -x;
return true;
}
inline bool R(double &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (ch == '.') {
double tmp = 1;
ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
tmp /= 10.0, x += tmp * (ch - '0');
}
if (sign)
x = -x;
return true;
}
inline bool R(char *s) {
char ch = nc();
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
for (; !blank(ch) && !IOerror; ch = nc())
*s++ = ch;
*s = 0;
return true;
}
inline bool R(char &c) {
c = nc();
if (IOerror) {
c = -1;
return false;
}
return true;
}
template <class T, class... U>
bool R(T &h, U &... tmp) { return R(h) && R(tmp...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
//洛谷P4462 [CQOI2018]异或序列
const int maxn = 2e5 + 5;
int n, Q, k, B;
int a[maxn], pos[maxn];
struct node {
int l, r, id;
bool operator<(const node &A) const { return pos[l] == pos[A.l] ? r < A.r : pos[l] < pos[A.l]; }
} q[maxn];
ll ans = 0, ANS[maxn];
int pre[maxn], cnt[maxn];
void update(int id, int f) {
int x = pre[id];
if (f == 1) {
ans += cnt[x ^ k];
cnt[x]++;
} else {
cnt[x]--;
ans -= cnt[x ^ k];
}
}
int main() {
R(n, Q, k);
B = sqrt(n);
rep(i, 1, n) {
R(a[i]);
pos[i] = i / B;
pre[i] = pre[i - 1] ^ a[i];
}
rep(i, 1, Q) {
R(q[i].l, q[i].r), q[i].id = i;
--q[i].l;
}
sort(q + 1, q + 1 + Q);
int l = 1, r = 0;
rep(i, 1, Q) {
while (l < q[i].l) update(l++, -1);
while (l > q[i].l) update(--l, 1);
while (r < q[i].r) update(++r, 1);
while (r > q[i].r) update(r--, -1);
ANS[q[i].id] = ans;
}
rep(i, 1, Q) W(ANS[i]);
}