Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

E. XOR and Favorite Number
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

Input

The first line of the input contains integers nm and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.

The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.

Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.

Output

Print m lines, answer the queries in the order they appear in the input.

Examples
input
6 2 3
1 2 1 1 0 3
1 6
3 5
output
7
0
input
5 3 1
1 1 1 1 1
1 5
2 4
1 3
output
9
4
4
Note

In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.

In the second sample xor equals 1 for all subarrays of an odd length.

 题意:找出区间有多少对异或和为k的对数;

思路:莫队算法,主要是如何o(1)更新,a[l]^a[l+1].....^a[r]=pre[r]^pre[l-1]=k;pre[i]表示a[1]^a[2]^....^a[i];

   pos[r]^pre[l-1]=k;   pre[r]=pre[l-1]^k;更新pre;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
int si[N];
struct is
{
    int l,r,pos;
    bool operator < (const is &b)const
    {
        if(si[l]==si[b.l])
        return r<b.r;
        return si[l]<si[b.l];
    }
}q[N];
int a[N];
int n,m,k;
int l,r;
ll out[N],ans;
int flag[M<<1];
void add(int pos)
{
    ans+=flag[k^a[pos]];
    flag[a[pos]]++;
}
void del(int pos)
{
    flag[a[pos]]--;
    ans-=flag[k^a[pos]];
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    int kuai=sqrt(n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]),si[i]=(i-1)/kuai+1,a[i]^=a[i-1];
    for(int i=1;i<=m;i++)
    scanf("%d%d",&q[i].l,&q[i].r),q[i].pos=i;
    sort(q+1,q+m+1);
    l=1;
    r=0;
    ans=0;
    flag[0]=1;
    for(int i=1;i<=m;i++)
    {
        while(l<q[i].l)
        {
            del(l-1);
            l++;
        }
        while(l>q[i].l)
        {
            l--;
            add(l-1);
        }
        while(r<q[i].r)
        {
            r++;
            add(r);
        }
        while(r>q[i].r)
        {
            del(r);
            r--;
        }
        out[q[i].pos]=ans;
    }
    for(int i=1;i<=m;i++)   printf("%lld\n",out[i]);
    return 0;
}

 

posted @ 2016-09-12 12:32  jhz033  阅读(169)  评论(0编辑  收藏  举报