luogu P4462 [CQOI2018] XOR sequence | Mo team

Title Description

Given a length of an integer number n columns a1, a2, ..., an, a given query parameter l, r, asked al, al + 1, ..., ar within the range, the number of different sequences to satisfy and or equal to k. That is, for all x, y (I ≤ x ≤ y ≤ r), can satisfy ax⨁ax + 1⨁ ... ⨁ay = ka_x \ bigoplus a_ {x + 1} \ bigoplus ... \ bigoplus a_y = kax ⨁ax + 1 ⨁ ... ⨁ay = k x, y, the number of groups.

Input Format

The first line of the input file, three integers n, m, k.

The second line n integers separated spaces, i.e., a1, a2, .. an.

Next m lines of two integers lj, rj, represents a query.

Output Format

Total output file m rows, each query corresponding to the calculation result.


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1e5+10;
int n,m,k,a[N],belong[N];
struct node{
    int l,r,id;
}e[N];
inline bool cmp(node t1,node t2){
    return (belong[t1.l]^belong[t2.l]) ? t1.l<t2.l : t1.r<t2.r;
}
int cnt[N],ans[N],sum;
inline void add(int x){
    sum+=cnt[a[x]^k];
    cnt[a[x]]++;
}
inline void del(int x){
    cnt[a[x]]--;
    sum-=cnt[a[x]^k];
}
signed main(){
    cin>>n>>m>>k;
    int size=sqrt(n);
    int num=ceil((double)n/size);
    for(int i=1;i<=num;i++)
    for(int j=(i-1)*size+1;j<=i*size;j++)belong[j]=i;
    for(int i=1;i<=n;i++){scanf("%d",&a[i]);a[i]^=a[i-1];}
    for(int i=1;i<=m;i++){scanf("%d%d",&e[i].l,&e[i].r);e[i].id=i;e[i].l--;}
    sort(e+1,e+m+1,cmp);
    int l=e[1].l,r=e[1].r;
    for(int i=l;i<=r;i++)add(i);
    for(int i=1;i<=m;i++){
        while(l<e[i].l)del(l++);
        while(l>e[i].l)add(--l);
        while(r<e[i].r)add(++r);
        while(r>e[i].r)del(r--);
        ans[e[i].id]=sum;
    }
    for(int i=1;i<=m;i++)
    printf("%d\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/11892440.html