E. XOR and Favorite Number
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.
题意:给一个数组,还有一个 k ,然后是 m 个询问,每次询问给 l 和 r . 问 [l, r] 有多少个子区间的异或值为 k .
思路:莫队裸题。有几点值得注意的,del 函数和 add 函数中的先后问题。具体可看代码
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()
using namespace std;
const int maxn = 2e6 + 10;
LL ans[maxn];
LL a[maxn],Ans = 0;
LL preXor[maxn],cnt[maxn];
LL n,m,k;
int belong[maxn];
struct xx{
int l,r,id;
}Q[maxn];
inline LL read() {
LL X = 0, w = 0;
char ch = 0;
while(!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while(isdigit(ch))
X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void write(LL x) {
if(x < 0)
putchar('-'), x = -x;
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
bool cmp(xx A,xx B){
if(belong[A.l] != belong[B.l])
return A.l < B.l;
return A.r < B.r;
}
void del(int x){
-- cnt[preXor[x]]; //如当k==0时,当前[L,R]区间preXor为6 0 6 0 6 0,先对Ans操作,就会多减
Ans -= cnt[preXor[x] ^ k];
}
void add(int x){
Ans += cnt[preXor[x] ^ k]; //如当k==0时,当前[L,R]区间preXor为6 0 6 0 6 0,先对cnt操作,就会多加
++ cnt[preXor[x]];
}
int main() {
while(~scanf("%lld%lld%lld",&n,&m,&k)){
LL block = sqrt(n);
for(int i = 1;i <= n;++ i){
a[i] = read();
preXor[i] = preXor[i - 1] ^ a[i];
belong[i] = (i - 1) / block + 1;
}
for(int i = 1;i <= m;++ i){
Q[i].l = read(); Q[i].r = read();
Q[i].id = i;
}
sort(Q + 1,Q + 1 + m,cmp);
int L = 1,R = 0;
clr(cnt,0); cnt[0] = 1;
Ans = 0;
for(int i = 1;i <= m;++ i){
while(L < Q[i].l) del(L - 1),L ++; /// 减1的原因是 算[L,R]的区间异或值是 preXor[R]^preXor[L - 1]
while(L > Q[i].l) -- L,add(L - 1);
while(R < Q[i].r) add(++ R);
while(R > Q[i].r) del(R --);
ans[Q[i].id] = Ans;
}
for(int i = 1;i <= m;++ i)
write(ans[i]),putchar('\n');
}
return 0;
}