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.
题目大意:给你n个数,m次查询:求区间 [l, r] 内异或值等于k的连续段的个数。
首先我们先来讲下什么是莫队算法:这个算法的作用就是可以在知道区间[l,r]内答案的情况下O(1)的时间内得出[l+1,r]、[l-1,r]、[l,r+1]、[l,r-1]的答案,然后把查询区间分成许多块,然后再根据区间的位置进行排序,最后根据莫队算法的性质,不断通过已知的答案更快的求出未知的答案。
解题思路:运用莫队算法和位运算来解决,这里我们还用到了一个位运算的技巧:a^a = 0, 0^a = a,所以求某段连续数的异或和可以用前缀和来处理,例如求区间 [l ,r]的异或和,我们可以先预处理出所有数异或和的前缀和,就可以得到sum[l, r] = sum[l-1]^sum[l-1]^sum[l, r] = sum[l-1] ^ sum[r],可以用一个类似vis的数组存区间的前缀和值异或直接得出答案。
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<double, int> dpar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const LL INF = 3e9+7;
const uLL base = 131;
const int N = 1010;
const double pi = 3.1415926;
int n, m, k, sz;
LL a[100010], b[100010], sum[100010], cnt[1<<20]; //记数数组记得开大点,异或的结果可能不止1e6
int L, R;
LL ans;
struct node
{
int l;
int r;
int id;
bool operator<(const node a)const { //对答案分块排序
if(a.l/sz == l/sz) return r < a.r;
return l < a.l;
}
}e[100010];
void del(int x)
{
cnt[x] --;
ans -= cnt[x^k];//删除答案
}
void add(int x)
{
ans += cnt[x^k];//增加答案
cnt[x] ++;
}
int main()
{
while(~scanf("%d%d%d", &n, &m, &k)) {
sum[0] = 0;
mem0(b);
mem0(cnt);
for(int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
sum[i] = sum[i-1]^a[i];
}
sz = ceil(sqrt(n*1.0));
for(int i = 1; i <= m; i ++) {
scanf("%d%d", &e[i].l, &e[i].r);
e[i].l --; //这里需要减1,因为我们某一段点的异或值为k^sum[L-1]
e[i].id = i;
}
L = 1;
R = 0; //不同的起点的初始化也可能不同
ans = 0;
sort(e+1, e+m+1);
for(int i = 1; i <= m; i ++) {
while(L < e[i].l) {
del(sum[L]);
L ++;
}
while(L > e[i].l) {
L --;
add(sum[L]);
}
while(R < e[i].r) {
R ++;
add(sum[R]);
}
while(R > e[i].r) {
del(sum[R]);
R --;
}
b[e[i].id] = ans;
}
for(int i = 1; i <= m; i ++) {
printf("%lld\n", b[i]);
}
}
return 0;
}