# bzoj5301: [Cqoi2018]异或序列（莫队）

2020-12-07 14:36:23

sum[i]表示1到i的异或和。

``````#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {

int l,r,id,ans;}a[110000];int f[110000],sum[110000],sumk[110000];
bool cmp(node n1,node n2) {

if(f[n1.l]==f[n2.l])return n1.r<n2.r;else return n1.l<n2.l;}
bool cmp1(node n1,node n2) {

return n1.id<n2.id;}
int main() {
int n,m,k;scanf("%d%d%d",&n,&m,&k);int unit=int(sqrt(double(n+1)));sum[0]=0;
for(int i=1;i<=n;i++) {

int x;scanf("%d",&x);f[i]=i/unit+1;sum[i]=sum[i-1]^x;}
for(int i=1;i<=m;i++)a[i].id=i;
for(int i=1;i<=m;i++)scanf("%d%d",&a[i].l,&a[i].r);sort(a+1,a+1+m,cmp);
int l=a[1].l,r=a[1].r;int ans=0;
for(int i=a[1].l;i<=a[1].r;i++) {
sumk[sum[i]]++;
int x=sum[i]^k;
ans+=sumk[x];if(x==sum[l-1])ans++;
}a[1].ans=ans;
for(int i=2;i<=m;i++) {
while(l<a[i].l) {
int x=sum[l-1]^k;
ans-=sumk[x];
sumk[sum[l]]--;l++;
}
while(l>a[i].l) {
l--;sumk[sum[l]]++;
int x=sum[l-1]^k;
ans+=sumk[x];
}
while(r<a[i].r) {
r++;sumk[sum[r]]++;
int x=sum[r]^k;
ans+=sumk[x];if(x==sum[l-1])ans++;
}
while(r>a[i].r) {
int x=sum[r]^k;
ans-=sumk[x];
if(x==sum[l-1])ans--;sumk[sum[r]]--;r--;
}a[i].ans=ans;
}sort(a+1,a+1+m,cmp1);
for(int i=1;i<=m;i++)printf("%d\n",a[i].ans);
return 0;
}``````

https://my.oschina.net/u/4390412/blog/4778631