BZOJ 5301: [Cqoi2018]异或序列

莫队

#include<cstdio>
#include<algorithm>
using namespace std;
int ans,n,m,k,lim=300,Sum[1000005],belong[1000005],ANS[1000005],S[1000005],a[1000005];
struct node{
	int l,r,id;
}e[1000005];
bool cmp(node a,node b){
	return belong[a.l]<belong[b.l] || belong[a.l]==belong[b.l] && a.r<b.r;
}
void update(int x,int cas){
	x=Sum[x];
	if (cas==-1){
		S[x]--;
		ans-=S[x^k];
	}
	else{
		ans+=S[x^k];
		S[x]++;
	}
}
int main(){
	scanf("%d%d%d",&n,&m,&k);
	for (int i=0; i<=n; i++) belong[i]=i/lim;
	for (int i=1; i<=n; i++){
		scanf("%d",&a[i]);
		Sum[i]=Sum[i-1]^a[i];
	}
	for (int i=1; i<=m; i++){
		scanf("%d%d",&e[i].l,&e[i].r);
		e[i].l--;
		e[i].id=i;
	}
	sort(e+1,e+m+1,cmp);
	int l=1,r=0;
	ans=0;
	for (int i=1; i<=m; i++){
		while (l>e[i].l) l--,update(l,1);
		while (l<e[i].l) update(l,-1),l++;
		while (r>e[i].r) update(r,-1),r--;
		while (r<e[i].r) r++,update(r,1);
		ANS[e[i].id]=ans;
	}
	for (int i=1; i<=m; i++)
		printf("%d\n",ANS[i]);
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/silenty/p/9787907.html