CodeForces - 617E XOR and Favorite Number

The meaning of the question: give you a sequence of length n, and give you a query m times, how many consecutive subsequences can be in each query interval and the XOR sum is k

Solution: It's still Team Mo, and it's violent to do things. Assuming a^b=k, then we only need to add the number of mp[a[x]^k] to ans every time we add the team (same for del). To process the prefix XOR sum, the XOR prefix sum in the query interval maintained by mp. Because the XOR of the two prefixes and the XOR can get the XOR of the middle paragraph

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iomanip>
#include <set>
#include<ctime>
//CLOCKS_PER_SEC
#define se second
#define fi first
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Pii pair<int,int>
#define Pli pair<ll,int>
#define ull unsigned long long
#define pb push_back
#define fio ios::sync_with_stdio(false);cin.tie(0)
const int N=1<<20;
const ull base=163;
const int INF=0x3f3f3f3f;
using namespace std;
ll a[N];
struct node {
    int l,r,id;
}Q[N];
int pos[N];
bool cmp(node x,node y){
    if(pos[x.l]==pos[y.l])return x.r<y.r;
    return pos[x.l]<pos[y.l];
}
int L = 1 , R = 0 ;
ll  ans=0;
ll res[N];
int n,q,k;
ll mp[N];
void del(int x){
    mp [a [x]] - ;
    ans -=mp[a[x]^ k];
}
void add(int x){
    ans +=mp[a[x]^ k];
    mp [a [x]] ++ ;
}
int main(){
    scanf("%d%d%d",&n,&q,&k);
    int sz=sqrt(n);
    for(int i=1;i<=n;i++){
        scanf("%I64d",&a[i]);
        a[i]=a[i]^a[i-1];
        pos[i]=i/sz;
    }
    for(int i=1;i<=q;i++){
        scanf("%d%d",&Q[i].l,&Q[i].r);
        Q[i].id=i;
    }
    sort(Q+1,Q+1+q,cmp);
    mp[0]=1;
    for(int i=1;i<=q;i++){
        while(L<Q[i].l){
            del (L - 1 );
            L++;
        }
        while(L>Q[i].l){
            L--;
            add(L-1);
        }
        while(R>Q[i].r){
            del(R);
            R--;
        }
        while(R<Q[i].r){
            R++;
            add(R);
        }
        res[Q[i].id]=ans;
    }
    for(int i=1;i<=q;i++){
        printf("%I64d\n",res[i]);
    }
    return 0;
}

 

Guess you like

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