BZOJ5301 [Cqoi2018]异或序列 【莫队】
题目链接
题解
莫队水题
BZOJ400AC纪念
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int bac[maxn],a[maxn],n,m,K,B;
LL ans[maxn];
struct Que{int l,r,b,id;}q[maxn];
inline bool operator <(const Que& a,const Que& b){
return a.b == b.b ? a.r < b.r : a.l < b.l;
}
void solve(){
sort(q + 1,q + 1 + m);
int L = q[1].l,R = q[1].r,tmp; LL cnt = 0;
for (int i = q[1].l; i <= q[1].r; i++){
tmp = a[i] ^ K;
cnt += bac[tmp];
bac[a[i]]++;
}
ans[q[1].id] = cnt;
for (int i = 2; i <= m; i++){
while (L != q[i].l || R != q[i].r){
if (L > q[i].l){
L--;
tmp = a[L] ^ K;
cnt += bac[tmp];
bac[a[L]]++;
}
if (L < q[i].l){
tmp = a[L] ^ K;
bac[a[L]]--;
cnt -= bac[tmp];
L++;
}
if (R < q[i].r){
R++;
tmp = a[R] ^ K;
cnt += bac[tmp];
bac[a[R]]++;
}
if (R > q[i].r){
tmp = a[R] ^ K;
bac[a[R]]--;
cnt -= bac[tmp];
R--;
}
}
ans[q[i].id] = cnt;
}
REP(i,m) printf("%lld\n",ans[i]);
}
int main(){
n = read(); m = read(); K = read(); B = (int)sqrt(n) + 1;
REP(i,n) a[i] = a[i - 1] ^ read();
REP(i,m) q[i].id = i,q[i].l = read() - 1,q[i].r = read(),q[i].b = q[i].l / B;
solve();
return 0;
}