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 thatl ≤ 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
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

假设我们现在有一个前缀异或和数组sum[],现在我们要求区间[L,R]的异或的值,

用sum数组表示就是sum[L-1]^sum[R]==K,或者说是K^sum[R]==sum[L-1]

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 2e6 + 10;
 5 int n, m, k, L, R, sz, a[maxn];
 6 LL sum[maxn], ans, ANS[maxn];
 7 struct node {
 8     int l, r, id;
 9     node() {}
10     node(int l, int r, int id): l(l), r(r), id(id) {}
11     bool operator <(const node & a)const {
12         if (l / sz == a.l / sz) return r < a.r;
13         return l < a.l;
14     }
15 } qu[maxn];
16 void add(int x) {
17     ans += sum[a[x] ^ k];
18     sum[a[x]]++;
19 }
20 void del(int x) {
21     sum[a[x]]--;
22     ans -= sum[a[x] ^ k];
23 }
24 int main() {
25     scanf("%d%d%d", &n, &m, &k);
26     for (int i = 1 ; i <= n ; i++) {
27         scanf("%d", &a[i]);
28         a[i] ^= a[i - 1];
29     }
30     for (int i = 1 ; i <= m ; i++) {
31         scanf("%d%d", &qu[i].l, &qu[i].r);
32         qu[i].l--;
33         qu[i].id = i;
34     }
35     sz = (int)sqrt(n);
36     sort(qu + 1, qu + m + 1);
37     L = 1, R = 0;
38     for (int i = 1 ; i <= m ; i++) {
39         while(L > qu[i].l) add(--L);
40         while(R < qu[i].r) add(++R);
41         while(L < qu[i].l) del(L++);
42         while(R > qu[i].r) del(R--);
43         ANS[qu[i].id] = ans;
44     }
45     for (int i = 1 ; i <= m ; i++)
46         printf("%lld
", ANS[i]);
47     return 0;
48 }
原文地址:https://www.cnblogs.com/qldabiaoge/p/9360505.html