Codeforces Round #340 (Div. 2) E 莫队+前缀异或和

时间:2023-03-10 04:46:14
Codeforces Round #340 (Div. 2) E 莫队+前缀异或和
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 n, m 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.

题意:给你一个长度为n的序列 q个查询[l,r]  问[l,r] 有多少个子区间的异或和为k

题解:莫队+前缀异或和

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#define ll __int64
using namespace std;
int n,m,k;
struct node
{
int l,r,id;
}N[];
int p[];
int block;
int a[];
int x[];
int mp[];
ll ans=;
ll re[];
int cmp(struct node aa,struct node bb)
{
if(p[aa.l]==p[bb.l])
return aa.r<bb.r;
else
return p[aa.l]<p[bb.l];
}
void update(int w,int h)
{
if(h==){
ans=ans+mp[x[w]^k];
mp[x[w]]++;
}
else
{
mp[x[w]]--;
ans=ans-mp[x[w]^k];
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
x[]=;
mp[]=;
for(int i=;i<=n;i++)
x[i]=a[i]^x[i-];
for(int i=;i<=m;i++){
scanf("%d %d",&N[i].l,&N[i].r);
N[i].id=i;
}
block=(int)sqrt((double)n);
for(int i=;i<=n;i++)
p[i]=(i-)/block+;
sort(N+,N++m,cmp);
ans=;
for(int i=,l=,r=;i<=m;i++)
{
for(;r<N[i].r;r++) update(r+,);
for(;l>N[i].l;l--) update(l-,);// 取异或的原因
for(;r>N[i].r;r--) update(r,-);
for(;l<N[i].l;l++) update(l-,-);//
re[N[i].id]=ans;
}
for(int i=;i<=m;i++)
printf("%I64d\n",re[i]);
return ;
}
/*
10 2 0
0 0 0 0 0 0 0 0 0 0
2 8
2 8
*/