bzoj5301[CQOI2018]异或序列

分析

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100005;
int a[maxn],s1[maxn],s2[maxn];
int cnt1[500000],cnt2[500000],ans;
int SZ=250;
struct query{
int l,r,num,ans;
scanf("%d%d",&l,&r);l--;
}
}Q[maxn];
bool cmp1(const query &A,const query &B){
if(A.l/SZ!=B.l/SZ)return A.l<B.l;
return A.r<B.r;
}
void init(int l,int r){
//printf("%d %d\n",l,r);

for(int i=l;i<=r;++i){
//printf("%d ",s1[i]);
cnt1[s1[i]]++;
}//printf("\n");
for(int i=l;i<=r;++i){
cnt2[s2[i]]++;//printf("%d ",s2[i]);
ans+=cnt1[s2[i]];
}//printf("\n");
//printf("%d\n",ans);
}
void move(int l1,int r1,int l2,int r2){
for(int i=l1;i<l2;++i){
ans-=cnt2[s1[i]];ans-=cnt1[s2[i]];
cnt1[s1[i]]--;cnt2[s2[i]]--;
if(s1[i]==s2[i])ans+=1;
}
for(int i=l1-1;i>=l2;--i){
ans+=cnt2[s1[i]];ans+=cnt1[s2[i]];
cnt1[s1[i]]++;cnt2[s2[i]]++;
if(s1[i]==s2[i])ans-=1;
}
for(int i=r1+1;i<=r2;++i){
ans+=cnt2[s1[i]];ans+=cnt1[s2[i]];
cnt1[s1[i]]++;cnt2[s2[i]]++;
if(s1[i]==s2[i])ans-=1;
}
for(int i=r1;i>r2;--i){
ans-=cnt2[s1[i]];ans-=cnt1[s2[i]];
cnt1[s1[i]]--;cnt2[s2[i]]--;
if(s1[i]==s2[i])ans+=1;
}
}
int res[maxn];
int main(){
int n,m,k;scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;++i){
scanf("%d",a+i);
}
for(int i=1;i<=n;++i)s1[i]=s1[i-1]^a[i];
for(int i=0;i<=n;++i)s2[i]=s1[i]^k;
for(int i=1;i<=m;++i)Q[i].num=i;
sort(Q+1,Q+m+1,cmp1);
init(Q[1].l,Q[1].r);Q[1].ans=ans;
for(int i=2;i<=m;++i){
move(Q[i-1].l,Q[i-1].r,Q[i].l,Q[i].r);
Q[i].ans=ans;
}
if(k==0){
for(int i=1;i<=m;++i)Q[i].ans-=(Q[i].r-Q[i].l+1);
}
for(int i=1;i<=m;++i)Q[i].ans/=2;
for(int i=1;i<=m;++i)res[Q[i].num]=Q[i].ans;
for(int i=1;i<=m;++i)printf("%d\n",res[i]);
return 0;
}

posted @ 2019-01-19 18:10  liu_runda  阅读(238)  评论(0编辑  收藏