首页 > 技术文章 > Codeforces617E(莫队)

Penn000 2017-11-14 21:23 原文

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

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.

 

题意:询问区间[l,r]内有多少个子区间,其亦或和等于k。

思路:莫队,对于区间[a,b],区间[a,b+1]的ans等于[a,b]的ans加上区间[a,b]内OXR[b+1]^k的个数

     对于[a,b]的亦或和,即为XOR[b]^XOR[a-1]

   XOR[b]^XOR[a-1] == k   <==>   XOR[b]^k == XOR[a-1]

   因此寻找有多少个XOR[a-1]满足XOR[b]^XOR[a-1] == k ,即寻找有多少个XOR[b]^k

   使用一个cnt数组记录当前状态下不同区间亦或和的值出现的次数。

 1 //2017-11-14
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 1100000;
10 const int LEN = 1000;
11 
12 int n, m, k, L, R, a[N], XOR[N], block[N];
13 long long ans, ANS[N], cnt[N];
14 struct Node{
15     int l, r, id;
16     bool operator<(const Node x) const {
17         if(block[l] == block[x.l])
18           return r < x.r;
19         return block[l] < block[x.l];
20     }
21 }q[N];
22 
23 void add(int x){
24     ans += cnt[XOR[x]^k];
25     cnt[XOR[x]]++;
26 }
27 
28 void del(int x){
29     cnt[XOR[x]]--;
30     ans -= cnt[XOR[x]^k];
31 }
32 
33 int main()
34 {
35     //freopen("input.txt", "r", stdin);
36     while(~scanf("%d%d%d", &n, &m, &k)){
37         XOR[0] = 0;
38         for(int i = 1; i <= n; i++){
39           scanf("%d", &a[i]);
40           XOR[i] = XOR[i-1] ^ a[i];
41           block[i] = (i-1)/LEN;
42         }
43         for(int i = 0; i < m; i++){
44             scanf("%d%d", &q[i].l, &q[i].r);
45             q[i].id = i;
46         }
47         sort(q, q+m);
48         L = 1, R = 0, ans = 0;
49         cnt[0] = 1;
50         for(int i = 0; i < m; i++){
51             while(L < q[i].l){
52                 del(L-1);
53                 L++;
54             }
55             while(L > q[i].l){
56                 L--;
57                 add(L-1);
58             }
59             while(R < q[i].r){
60                 R++;
61                 add(R);
62             }
63             while(R > q[i].r){
64                 del(R);
65                 R--;
66             }
67             ANS[q[i].id] = ans;
68         }
69         for(int i = 0; i < m; i++)
70           printf("%lld\n", ANS[i]);
71     }
72 
73     return 0;
74 }

 

推荐阅读