bzoj5301: [Cqoi2018]异或序列【莫队】

传送门

解题思路:

莫队算法,记录异或前缀和及桶即可。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}
const int N=100005;
int n,m,k,S,cnt[N],a[N];
ll ans[N];
struct node
{
    int l,r,id;
    inline friend bool operator < (const node &a,const node &b)
    {
        if(a.l/S==b.l/S)return a.r<b.r;
        return a.l/S<b.l/S;
    }
}q[N];
int main()
{
    //freopen("xor.in","r",stdin);
    //freopen("xor.out","w",stdout);
    n=getint(),m=getint(),k=getint(),S=sqrt(n);
    for(int i=1;i<=n;i++)a[i]=getint()^a[i-1];
    for(int i=1;i<=m;i++)q[i].l=getint()-1,q[i].r=getint(),q[i].id=i;
    sort(q+1,q+m+1);
    int l=1,r=0;ll cur=0;
    for(int i=1;i<=m;i++)
    {
        while(l<q[i].l)cnt[a[l]]--,cur-=cnt[k^a[l]],l++;
        while(l>q[i].l)l--,cur+=cnt[k^a[l]],cnt[a[l]]++;
        while(r<q[i].r)r++,cur+=cnt[k^a[r]],cnt[a[r]]++;
        while(r>q[i].r)cnt[a[r]]--,cur-=cnt[k^a[r]],r--;
        ans[q[i].id]=cur;
    }
    for(int i=1;i<=m;i++)cout<<ans[i]<<'\n';
}

猜你喜欢

转载自blog.csdn.net/cdsszjj/article/details/80196490