CF617E. XOR and Favorite Number

 1 /*
 2  CF617E. XOR and Favorite Number
 3  http://codeforces.com/contest/617/problem/E
 4  莫队算法
 5  题意:求l,r区间内异或和为k的对数
 6  用times记录某异或值在当前区间中的个数
 7  */
 8 #include <cstdio>
 9 #include <algorithm>
10 #include <cstring>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 //#define test
15 using namespace std;
16 const int Nmax=1<<20;
17 struct Q
18 {
19     int l,r,id;
20 }q[Nmax];
21 long long ans[Nmax];
22 long long Ans;
23 int l=1,r=0;
24 int n,m,k;
25 int num[Nmax];
26 int pos[Nmax];
27 long long times[Nmax];
28 bool cmp(Q a,Q b)
29 {
30     if(pos[a.l]==pos[b.l])
31         return a.r<b.r;
32     return pos[a.l]<pos[b.l];
33 }
34 void del(int x)
35 {
36     times[num[x]]--;
37     Ans-=times[num[x]^k];
38 }
39 void add(int x)
40 {
41     Ans+=times[num[x]^k];
42     times[num[x]]++;
43 }
44 int main()
45 {
46     #ifdef test
47     #endif
48     scanf("%d%d%d",&n,&m,&k);
49     int sz=sqrt(n);
50     for(int i=1;i<=n;i++)
51     {
52         scanf("%d",&num[i]);
53         num[i]^=num[i-1];
54         pos[i]=i/sz;
55     }
56     for(int i=1;i<=m;i++)
57     {
58         scanf("%d%d",&q[i].l,&q[i].r);
59         q[i].id=i;
60     }
61     sort(q+1,q+1+m,cmp);
62     times[0]=1LL;
63     for(int i=1;i<=m;i++)
64     {
65         while(l<q[i].l)
66         {
67             del(l-1);
68             l++;
69         }
70         while(l>q[i].l)
71         {
72             l--;
73             add(l-1);
74         }
75         while(r<q[i].r)
76         {
77             r++;
78             add(r);
79         }
80         while(r>q[i].r)
81         {
82             del(r);
83             r--;
84         }
85         ans[q[i].id]=Ans;
86     }
87     for(int i=1;i<=m;i++)
88         printf("%lld\n",ans[i]);
89     return 0;
90 }

 

posted @ 2017-03-28 20:39  BBBob  阅读(353)  评论(0编辑  收藏  举报