码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 617E XOR and Favorite Number(莫队)

时间:2018-02-23 10:44:19      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:copy   data-   pairs   with   order   spec   pre   index   情况   

题目链接:http://codeforces.com/problemset/problem/617/E

题目:

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
Copy
6 2 3
1 2 1 1 0 3
1 6
3 5
output
7
0
input
Copy
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.

题意:给定n个元素,q次询问,每次询问【l,r】区间内ai^...^aj=k有几对。

题解:先预处理出前缀的ai。ai^...^aj=sum[i-1]^sum[j]=k。sum[i-1]^k=sum[j]。然后就是基本的莫队操作了。add和del那里两个东西哪个再前面,这个其实想一想k=0的情况就很好理解了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 
 8 typedef long long LL;
 9 
10 const int N=3e6+10;
11 struct node{
12     int l,r,id;
13 }Q[N];
14 
15 LL a[N],ans[N],cnt[N];
16 int pos[N],BLOCK;
17 bool cmp(node x,node y){
18     if(pos[x.l]==pos[y.l]) return x.r<y.r;
19     return pos[x.l]<pos[y.l];
20 }
21 
22 int n,m,k;
23 LL Ans=0;
24 
25 void add(int x){
26     Ans+=cnt[a[x]^k];
27     cnt[a[x]]++;
28 }
29 
30 void del(int x){
31     cnt[a[x]]--;
32     Ans-=cnt[a[x]^k];
33 }
34 
35 int main(){
36     scanf("%d%d%d",&n,&m,&k);
37     BLOCK=sqrt(n);
38     for(int i=1;i<=n;i++){
39         scanf("%lld",&a[i]);
40         pos[i]=i/BLOCK;
41         a[i]^=a[i-1];
42     }
43     for(int i=1;i<=m;i++){
44         scanf("%d%d",&Q[i].l,&Q[i].r);
45         Q[i].id=i;
46     }
47     sort(Q+1,Q+1+m,cmp);
48     int L=1,R=0;
49     cnt[0]=1;
50     for(int i=1;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=1;i<=m;i++)
70     printf("%lld\n",ans[i]);
71     return 0;
72 }

 

Codeforces 617E XOR and Favorite Number(莫队)

标签:copy   data-   pairs   with   order   spec   pre   index   情况   

原文地址:https://www.cnblogs.com/Leonard-/p/8460648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!