BZOJ5301: [CQOI2018] XOR Sequence - Problem Solving

https://www.lydsy.com/JudgeOnline/problem.php?id=5301

https://www.luogu.org/problemnew/show/P4462

Given an integer sequence a[1],a[2],…,a[n] of length n, given query parameters l, r, how many consecutive subsequences in the interval [l,r] satisfy The XOR sum is equal to k .
That is to say, for all x, y (l≤x≤y≤r), how many groups of x and y can satisfy a[x]^a[x+1]^...^a[y]=k.

At first I was thinking that it wasn't a chairman tree (funny).

Thinking too much, Mo team is enough to solve it.

In order to facilitate the interval XOR sum, a is treated as the prefix XOR sum.

Let's look at the code for the rest, it's not easy to say, just note that the movement of the left endpoint is to add/delete the point before it, because the XOR of l~r = a[r]^a[l-1].

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+5;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
struct qu {
     int pos, l, r;
}q[N];
int a[N],ans[N],cnt[N],sum,n,m,k,s;
inline int bel(int x){return (x-1)/s+1;}
bool cmp(qu b,qu c){
    return bel(b.l)==bel(c.l)?b.r<c.r:b.l<c.l;
}
inline void add(int x){
    sum +=cnt[x^ k];
    cnt[x]++;
}
inline void del(int x){
    cnt[x]--;
    sum = cnt[x^ k];
}
int main(){
    n=read(),m=read(),k=read();
    s=sqrt(n);
    for(int i=1;i<=n;i++)a[i]=a[i-1]^read();
    for(int i=1;i<=m;i++){
    q[i].pos=i;q[i].l=read();q[i].r=read();
    }
    sort(q+1,q+m+1,cmp);
    int ql=1,qr=0;cnt[0]++;
    for(int i=1;i<=m;i++){
    while(qr<q[i].r)add(a[++qr]);
    while(qr>q[i].r)del(a[qr--]);
    while(ql<q[i].l)del(a[ql-1]),ql++;
    while(ql>q[i].l)ql--,add(a[ql-1]);
    ans[q[i]pos] = sum;
    }
    for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940212&siteId=291194637