http://codeforces.com/contest/617/problem/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
Copy
6 2 3 1 2 1 1 0 3 1 6 3 5
output
Copy
7 0
input
Copy
5 3 1 1 1 1 1 1 1 5 2 4 1 3
output
Copy
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=0, 假设正好a[x] = 3 a[y] = 3 3^3 = 0 ans = 1 如果flag不先-- ans会变为-1 而ans的正确值为0
假设正好a[x] = 3 a[y] = 3 a[z] = 3 ans = 3 如果flag不先-- ans会变为0 而ans的正确值为1
*/
#include<bits/stdc++.h>
#define rep(i, j, k) for(int i=j; i<k; i++)
#define ll long long
using namespace std;
const int maxn =1<<20;
struct node{
int l, r, id;
}Q[maxn];
int pos[maxn];
ll ans[maxn];
ll flag[maxn];
int a[maxn];
bool cmp(node a, node b){
return pos[a.l]<pos[b.l] || pos[a.l] == pos[b.l] && a.r < b.r;
}
int n,m,k; int L=1, R=0; ll Ans=0;
void add(int x){
Ans+=flag[a[x]^k];
flag[a[x]]++;
}
void del(int x){
flag[a[x]]--;//必须先-- 见上方叙述
Ans-=flag[a[x]^k];
}
int main(){
scanf("%d%d%d", &n, &m, &k);
int sz = sqrt(n);
rep(i, 1, n+1){
scanf("%d", &a[i]);
a[i] = a[i]^a[i-1];
pos[i] = i/sz;
}
rep(i, 0, m){
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q, Q+m, cmp);
flag[0] = 1;
rep(i, 0, m){
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);
}
ans[Q[i].id] = Ans;
}
rep(i, 0 ,m){
cout<<ans[i]<<endl;
}
}
题意是求给定的区间内有多少种情况是符合 ai^ai+1^…^aj = k.
令a[x] 为a[1]^a[2]^a[3]^...^a[x]的前缀异或值, 区间[l,r]的异或值是a[l-1]^a[r]
莫对算法
就是有个全局的L R,像个螃蟹一样