CodeForces 617E XOR and Favorite Number
莫队算法。
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int maxn=+;
- int a[maxn],pre[maxn];
- long long cnt[*maxn];
- int pos[maxn];
- int n,m,k;
- long long ans[maxn];
- long long Ans;
- int L,R;
- struct X
- {
- int l,r,id;
- }s[maxn];
- bool cmp(const X&a,const X&b)
- {
- if(pos[a.l]==pos[b.l]) return a.r<b.r;
- return a.l<b.l;
- }
- int main()
- {
- scanf("%d%d%d",&n,&m,&k);
- int sz=sqrt(n);
- for(int i=;i<=n;i++)
- {
- scanf("%d",&a[i]);
- pre[i]=(pre[i-]^a[i]);
- pos[i]=i/sz;
- }
- for(int i=;i<=m;i++)
- {
- scanf("%d%d",&s[i].l,&s[i].r);
- s[i].id=i;
- }
- sort(s+,s++m,cmp);
- Ans=;
- cnt[pre[s[].l-]]++;
- for(int i=s[].l;i<=s[].r;i++)
- {
- Ans=Ans+cnt[pre[i]^k];
- cnt[pre[i]]++;
- }
- L=s[].l; R=s[].r;
- ans[s[].id]=Ans;
- for(int i=;i<=m;i++)
- {
- while(L<s[i].l)
- {
- cnt[pre[L-]]--;
- Ans=Ans-cnt[pre[L-]^k];
- L++;
- }
- while(L>s[i].l)
- {
- L--;
- Ans=Ans+cnt[pre[L-]^k];
- cnt[pre[L-]]++;
- }
- while(R<s[i].r)
- {
- R++;
- Ans=Ans+cnt[pre[R]^k];
- cnt[pre[R]]++;
- }
- while(R>s[i].r)
- {
- cnt[pre[R]]--;
- Ans=Ans-cnt[pre[R]^k];
- R--;
- }
- ans[s[i].id]=Ans;
- }
- for(int i=;i<=m;i++)
- printf("%lld\n",ans[i]);
- return ;
- }
CodeForces 617E XOR and Favorite Number的更多相关文章
- CodeForces - 617E XOR and Favorite Number (莫队+前缀和)
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is g ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- [CQOI 2018]异或序列&[Codeforces 617E]XOR and Favorite Number
Description 题库链接1 题库链接2 已知一个长度为 \(n\) 的整数数列 \(a_1,a_2,\cdots,a_n\) ,给定查询参数 \(l,r\) ,问在 \([l,r]\) 区间内 ...
- Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- CODEFORCES 340 XOR and Favorite Number 莫队模板题
原来我直接学的是假的莫队 原题: Bob has a favorite number k and ai of length n. Now he asks you to answer m queries ...
- XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和
CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! = j) ...
随机推荐
- oracle一次删除多张表
通过拼接sql语句来完成 例如有如下个表 想一次性删除,执行如下语句: select 'drop table '||table_name ||';' as dropsql from USER_TABL ...
- java获取程序执行时间
第一种是以毫秒为单位计算的. //伪代码 long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 lon ...
- Hibernate 系列教程11-继承-Single Table策略
Single Table策略 通过 discriminator鉴别器来区分是父类还是子类 Employee public class Employee { private Long id; priva ...
- 配置App真机测试证书的流程 一览
原文链接:http://www.jianshu.com/p/6b0de0d4c925 有开发者账号的前提下, 请进行如下步骤:1.首先登录网站:https://developer.apple.com. ...
- UVA 796 Critical Links (tarjan算法求割边)
这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...
- Python基础学习6---存储器
Python提供一个标准的模块,称为 pickle .使用它你可以在一个文件中储存任何Python对象,之后你又可以把它完整无缺地取出来.这被称为 持久地 储存对象.还有另一个模块称为 cPickle ...
- JSP 语法/标签
┣1.declaration Declaration定义了JSP脚本语言使用的变量和函数,这类似于Java中定义全局变量,或可以把它想像成pascal编程语言中的单元文件的interface部分.声明 ...
- Mie散射 文献图片
Technorati 标签: Mie Scattering,遥感,Remote Sensing
- robot_framewok自动化测试
robot_framewok自动化测试 http://wenku.baidu.com/view/691abcaa4b73f242336c5fec.html 接口自动化测试框架设计 http://wen ...
- ssh能够连接而sftp不能连接的解决方法
ssh能够连接而sftp不能连接的解决方法 昨天开始用FileZilla一直不能登录远程的服务器,ssh的登录就OK,因为是服务器,也不敢乱动.查了好多资料终于解决了. 首先,查看一下系统的安全日 ...