异或序列(题解)(莫队)
Junlier良心莫队
题目
luoguP4462 [CQOI2018]异或序列
code
#include<bits/stdc++.h>
#define lst long long
#define ldb double
#define N 100050
using namespace std;
const int Inf=1e9;
int read()
{
int s=0,m=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')m=1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return m?-s:s;
}
int n,m,K,unit;lst ans;
int v[N],S[N],bl[N],sum[N];lst Ans[N];
struct QUES{int le,ri,id;}ljl[N];
bool cmp(QUES &a,QUES &b){return bl[a.le]==bl[b.le]?a.ri<b.ri:a.le<b.le;}
void Update(int now,int xx)
{
int X=S[now];
if(xx==1)sum[X]++,ans+=sum[K^X];
else ans-=sum[K^X],sum[X]--;
}
int main()
{
n=read(),m=read(),K=read(),unit=sqrt(n);
for(int i=1;i<=n;++i)
v[i]=read(),S[i]=S[i-1]^v[i],bl[i]=i/unit+1;
for(int i=1;i<=m;++i)ljl[i]=(QUES){read()-1,read(),i};
sort(ljl+1,ljl+m+1,cmp);
int ll=1,rr=0;
for(int i=1;i<=m;++i)
{
while(ll<ljl[i].le)Update(ll++,-1);
while(ll>ljl[i].le)Update(--ll, 1);
while(rr<ljl[i].ri)Update(++rr, 1);
while(rr>ljl[i].ri)Update(rr--,-1);
Ans[ljl[i].id]=ans;
}
for(int i=1;i<=m;++i)
printf("%lld
",Ans[i]);
return 0;
}