CodeForces-617E XOR And Favorite Numbers(莫队)

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, ..., ajis 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.

题解:首先要知道 a^b=c 等价于 a=c^b;   我们用a[i]记录前I个数的异或和,然后离线处理所有区间(对于所有区间我们按L所在块为第一排序,该询问的r为第二排序,对所有询问区间排序);

对于新增加的一个数我们加上前区间异或等于k^s的数的个数,然后更新一下异或为s的数量,对于一个需要去掉的数,我需要先新更新这个数的数量,然后减去k^s的数量即可;

离线跑一遍,

参考代码为:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define LL long long
 7 using namespace std;
 8 const int Max = 1100000;
 9 const int MAXM = 1<<22;
10 struct Point
11 {
12     int L ,R,Id;
13 } a[Max];
14 
15 LL sum[Max],ans[Max],k;
16 int n,m,L,R;
17 LL cnt[MAXM],ant;
18 bool cmp(Point b,Point c)
19 {
20     return b.L/400==c.L/400? b.R<c.R:b.L<c.L;
21 }
22 void Dec(LL s) 
23 {
24     --cnt[s];
25     ant-=cnt[s^k];
26 }
27 void Inc(LL s)
28 {
29     ant += cnt[s^k];
30     cnt[s]++;
31 }
32 int main()
33 {
34     scanf("%d %d %lld",&n,&m,&k);
35     LL data;
36     for(int i=1;i<=n;i++) scanf("%lld",&sum[i]),sum[i]^=sum[i-1];    
37     for(int i=1;i<=m;i++) scanf("%d %d",&a[i].L,&a[i].R),a[i].Id = i,a[i].L--;
38     sort(a+1,a+m+1,cmp);
39     L=0,R=0,cnt[0]=1,ant=0;
40     for(int i=1;i<=m;i++)
41     {
42         while(R<a[i].R) Inc(sum[++R]);
43         while(R>a[i].R) Dec(sum[R--]);      
44         while(L<a[i].L) Dec(sum[L++]);       
45         while(L>a[i].L) Inc(sum[--L]);        
46         ans[a[i].Id]=ant;
47     }
48     for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
49     return 0;
50 }
View Code

 

posted @ 2018-08-19 23:06  StarHai  阅读(249)  评论(0编辑  收藏  举报