CF617E. XOR and Favorite Number
生活随笔
收集整理的這篇文章主要介紹了
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 }
?
轉載于:https://www.cnblogs.com/BBBob/p/6636745.html
總結
以上是生活随笔為你收集整理的CF617E. XOR and Favorite Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 79 Word Sea
- 下一篇: NSArray文件读写