返回顶部

莫队算法模板

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
#define A first
#define B second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define dd(x) cout << #x << " = " << (x) << ", "
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using namespace std;
//**********************************
const int maxn=1<<20;
int n,m,k;
int belong[maxn];
ll flag[maxn],ans[maxn],Ans;
int a[maxn];
int L=1,R=0;//下标从1开始 
struct Node{
    int l,r,id;
}q[maxn];
bool cmp(Node a,Node b)
{
    if(belong[a.l]!=belong[b.l])return belong[a.l]<belong[b.l];
    return a.r<b.r;
}
void insert(int x)
{
    Ans+=flag[a[x]^k];//<-----注意顺序
    flag[a[x]]++;      
}
void erase(int x)
{
    flag[a[x]]--;   //<-----注意顺序                                                        
    Ans-=flag[a[x]^k]; 
}
//**********************************
//**********************************
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    int s=sqrt(n);
    FOR(i,1,n)scanf("%d",&a[i]),a[i]=a[i]^a[i-1],belong[i]=i/s;
    FOR(i,1,m)scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
    sort(q+1,q+1+m,cmp);
    flag[0]=1;
    FOR(i,1,m){
        while(L<q[i].l)erase(L++-1);
        while(L>q[i].l)insert(--L-1);
        while(R<q[i].r)insert(++R);
        while(R>q[i].r)erase(R--);
        ans[q[i].id]=Ans;
        //erase都是后缀减
        //insert都是前缀加 
    }

    FOR(i,1,m)cout<<ans[i]<<endl;
    return 0;
}

 

posted @ 2018-08-30 12:25  牛奶加咖啡~  阅读(134)  评论(0编辑  收藏  举报