cqoi2018

题解:

很多模板题

第一次写莫队还比较顺利

除了把排序的cmp写错。。(还第一次遇到)

#include <bits/stdc++.h>

using namespace std;

#define rint register int

#define IL inline

#define rep(i,h,t) for (int i=h;i<=t;i++)

#define dep(i,t,h) for (int i=t;i>=h;i--)

#define ll long long 

#define me(x) memset(x,0,sizeof(x))

namespace IO

{

    char ss[1<<24],*A=ss,*B=ss;

    IL char gc()

    {

        return A==B&&(B=(A=ss)+fread(ss,1,1<<24,stdin),A==B)?EOF:*A++;

    }

    template<class T>void read(T &x)

    {

        rint f=1,c; while (c=gc(),c<48||c>57) if (c=='-') f=-1; x=(c^48);

        while (c=gc(),c>47&&c<58) x=(x<<3)+(x<<1)+(c^48); x*=f;

    }

    char sr[1<<24],z[20]; int Z,C=-1;

    template<class T>void wer(T x)

    {

        if (x<0) sr[++C]='-',x=-x;

        while (z[++Z]=x%10+48,x/=10);

        while (sr[++C]=z[Z],--Z);

    }

    IL void wer1()

    {

        sr[++C]=' ';

    }

    IL void wer2()

    {

        sr[++C]='\n';

    }

    template<class T>IL void maxa(T &x,T y) {if (x<y) x=y;}

    template<class T>IL void mina(T &x,T y) {if (x>y) x=y;}

    template<class T>IL T MAX(T x,T y){return x>y?x:y;}

    template<class T>IL T MIN(T x,T y){return x<y?x:y;}

};

using namespace IO;

const int N=1e5+10;

const int M=350;

int n,m,k,block,q;

int a[N],pos[N],b[N],now1[N],now2[N];

ll ans2[N];

struct re{

    int a,b,c;

}p[N];

ll ans=0;

bool cmp(re x,re y)

{

    return pos[x.a]<pos[y.a]||(pos[x.a]==pos[y.a]&&x.b<y.b);

}

IL void insert1(int x,int y)

{

    if (y==1)

    {

      now2[b[x]]+=y;

      now1[b[x-1]]+=y;

    }

    ans+=now2[b[x-1]^k]*y;

    if (y==-1)

    {

      now2[b[x]]+=y;

      now1[b[x-1]]+=y;

    }

}

IL void insert2(int x,int y)

{

    if (y==1)

    {

        now2[b[x]]+=y;

        now1[b[x-1]]+=y;

    }

    ans+=now1[b[x]^k]*y;

    if (y==-1)

    {

        now2[b[x]]+=y;

        now1[b[x-1]]+=y;

    }

}

int main()

{

    freopen("1.in","r",stdin);

    freopen("1.out","w",stdout);

    read(n); read(q); read(k);

    rep(i,1,n) read(a[i]);

    block=sqrt(n);

    m=(n-1)/block+1;

    rep(i,1,n) pos[i]=(i-1)/block+1;

    rep(i,1,q)

      read(p[i].a),read(p[i].b),p[i].c=i;

    sort(p+1,p+q+1,cmp);

    rep(i,1,n) b[i]=b[i-1]^a[i];

    rep(i,p[1].a,p[1].b)

    {

        now1[b[i-1]]++;

        ans+=now1[b[i]^k];

        now2[b[i]]++;

    }

    ans2[p[1].c]=ans;

    rep(i,2,q)

    {

        if (p[i].a>p[i-1].a)

          rep(j,p[i-1].a,p[i].a-1)

            insert1(j,-1);

        else dep(j,p[i-1].a-1,p[i].a) insert1(j,1);

        if (p[i].b>p[i-1].b)

          rep(j,p[i-1].b+1,p[i].b) insert2(j,1);

        else dep(j,p[i-1].b,p[i].b+1) insert2(j,-1);

        ans2[p[i].c]=ans;

    }

    rep(i,1,q) wer(ans2[i]),wer2();

    fwrite(sr,1,C+1,stdout);

    return 0;

}

猜你喜欢

转载自www.cnblogs.com/yinwuxiao/p/10018490.html