E. XOR and Favorite Number
题意:很多询问,求每个询问下,有多少个区间,异或=k。
分析:异或也有前缀和。[L,R] = pre[R] ^ pre[L-1];
莫队算法:是莫涛队长发明的,一种改良版的暴力离线算法。
首先将问题重新排序,有生成树的,有简单版的分块,然后通过一个区间去递推另个一区间的值。
这里需要记录一下flag[ pre[i] ] 的个数。
#include <bits/stdc++.h> using namespace std; const int maxn = <<;
int a[maxn]; struct Node {
int l,r,id;
}Q[maxn]; int pos[maxn];
long long ans[maxn];
long long flag[maxn]; 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];
} int n,m,k;
int L = ,R = ;
long long Ans; 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);
int sz = sqrt(n);
for(int i=; i <= n; i++) {
scanf("%d",&a[i]);
a[i] = a[i]^a[i-];
pos[i] = i/sz;
} for(int i=; i<=m ;i++) {
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id = i;
} sort(Q+,Q+m+,cmp);
flag[] = ; for(int i=; i <=m; i++) { while(L<Q[i].l) {
del(L-);
L++;
} while(L>Q[i].l) {
L--;
add(L-);
} while(R<Q[i].r) {
R++;
add(R);
} while(R>Q[i].r) {
del(R);
R--;
} ans[Q[i].id] = Ans;
} for(int i=; i <= m; i++)
printf("%I64d\n", ans[i]); return ;
}
E. XOR and Favorite Number的更多相关文章
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- 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 Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- XOR and Favorite Number (莫对算法)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- codeforces 617E E. XOR and Favorite Number(莫队算法)
题目链接: E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes i ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- 题解-CF617E XOR and Favorite Number
题面 CF617E XOR and Favorite Number 给定 \(n,m,k\) 和 \(n\) 个数的序列 \(a_i\),\(m\) 次求区间 \([l,r]\) 中异或值为 \(k\ ...
随机推荐
- 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器
5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...
- linux文件夹权限问题
linux下 ls 某文件夹需要文件夹有 r 读权限. cd 某文件夹 需要文件夹有 x 运行权限 参考: http://www.linuxidc.com/Linux/2016-11/136959.h ...
- 将 flask 中的 session 存储到 SQLite 数据库中
将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...
- vim创建新的命令
转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...
- 很有用的PHP笔试题系列二
1.如何用php的环境变量得到一个网页地址的内容?ip地址又要怎样得到? Gethostbyname() echo $_SERVER ["PHP_SELF"];echo $_SER ...
- 全局数据 GetGlobalDataSet
/// <summary> /// 获取全局数据 /// </summary> /// <returns></returns> public DataS ...
- 学习安卓开发过程中遇到关于R.Java文件的问题
在学习安卓开发过程时,遇到R.java生成问题,总结几个方法解决. 1.首先必须做的就是检查代码的正确性,存在错误的代码,不编译生成R.java 2.右键点项目,选择 Android Tools -& ...
- /Date(1410019200000+0800)/如何转换为date对象
<script type="text/javascript">var s = '/Date(1410019200000+0800)/ '; s.replace(/Dat ...
- My first python application
''' Authon:WSE_Gordon This application is use for the costomer to login the application. The Costome ...
- js动画杂记
在画布上做动画 方法有 setIntervel(function(){},time); setTimeout(function(){},time); 新方法 window.requestAnimati ...