【CodeForces617E】XOR and Favorite Number
来源:互联网 发布:网络推广平均工资 编辑:程序博客网 时间:2024/06/09 19:12
题目链接:
http://codeforces.com/problemset/problem/617/E
题解:
莫队算法
考虑如何进行转移,对于异或运算有一个性质,即
那么我们就可以维护一个异或的前缀和
用
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define LL long long#define reg register intusing namespace std;const int N=1<<20;int n,m,k,sz,L=1,R=0;LL ans[N],flag[N],Ans=0;int a[N],pos[N];struct node{ int l,r,id;}Q[N];bool cmp(node a,node b){ if(pos[a.l]==pos[b.l]) return a.r<b.r; return pos[a.l]<pos[b.l]; }void add(int x){ Ans+=flag[a[x]^k]; flag[a[x]]++;}void del(int x){ flag[a[x]]--; Ans-=flag[a[x]^k];}int main(){ scanf("%d%d%d",&n,&m,&k); sz=sqrt(n*1.0); for(reg i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]=a[i]^a[i-1]; pos[i]=i/sz; } for(reg i=1;i<=m;i++) { scanf("%d%d",&Q[i].l,&Q[i].r); Q[i].id=i; } flag[0]=1; sort(Q+1,Q+m+1,cmp); for(reg i=1;i<=m;i++) { while(L<Q[i].l) del(L++-1); while(L>Q[i].l) add(--L-1); while(R<Q[i].r) add(++R); while(R>Q[i].r) del(R--); ans[Q[i].id]=Ans; } for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); return 0; }
阅读全文
0 0
- 【CodeForces617E】XOR and Favorite Number
- [Codeforces617E]XOR and Favorite Number(莫队)
- 莫队算法 Codeforces617E XOR and Favorite Number
- XOR and Favorite Number
- cf XOR and Favorite Number
- [CF617E]XOR and Favorite Number
- E. XOR and Favorite Number
- Codeforces 617E XOR and Favorite Number
- 【codeforce】XOR and Favorite Number 莫队
- Codeforces 617E XOR and Favorite Number
- codeforces 617E XOR and Favorite Number
- Codeforces 617E XOR and Favorite Number 【莫队】
- CF 617E(XOR and Favorite Number-莫队算法)
- Codeforces Round #340 (Div. 2) E XOR and Favorite Number
- Codeforces617 E . XOR and Favorite Number(莫队算法)
- Codeforces 617E:XOR and Favorite Number 莫队算法
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number
- codeforces 617E XOR and Favorite Number (莫队)
- 数据结构与算法分析--1.1 解决选择问题
- Linux下getopt()函数的简单使用
- PHP开发环境部署
- Composer和laravel的理解
- B
- 【CodeForces617E】XOR and Favorite Number
- MySQL基础
- 自我实现itoa(整形转字符串)
- Linux学习之基础篇_2017.8.4
- Ceres(3)---Interfacing with Automatic Differentiation
- 数据结构顺序栈
- JavaScript--引用数据类型--objiect
- 从一个字符数组中读出相应的整数、实数
- Protobuf数据格式解析