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\ ...
随机推荐
- python 学习笔记一——Python安装和IDLE使用
好吧,一直准备学点啥,前些日子也下好了一些python电子书,但之后又没影了.年龄大了,就是不爱学习了.那就现在开始吧. 安装python 3 Mac OS X会预装python 2,Linux的大多 ...
- selenium原理和尝试
引用文章:https://www.cnblogs.com/Albert-Lee/p/6238866.html Selenium是一个自动化测试框架.因为它能够模拟人工操作,比如能在浏览器中点击按钮.在 ...
- java String类型转 java.sql.time类型
String[] timePhase = reservationRuleInDTO.getTimePhase().split(",");List<ReservationTim ...
- Python归纳 | 爬虫基础知识
1. urllib模块库 Urllib是python内置的HTTP请求库,urllib标准库一共包含以下子包: urllib.error 由urllib.request引发的异常类 urllib.pa ...
- 安装wine
sudo add-apt-repository ppa:ubuntu-wine/ppa sudo apt-get update sudo apt-get install winetricks
- Excel&&word&&PPT
1. Excel 1.1 制作下拉框 选中单元格或列--> 菜单"数据" --> "数据验证"-->"设置" --> ...
- pat00-自测4. Have Fun with Numbers (20)
00-自测4. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yu ...
- NetCDF 共享软件 中文
NetCDF 共享软件 转载 在 Models-3 模式中,使用的数据存取接口称为 I/O API,其实就是 NetCDF 文件格式.而由于我们需要了解 Models-3 输出档案的数据情况,因此 ...
- win10的xbox下载应用或者游戏时,出现0x80070422和0x80073D0A的解决办法
这个错误:0x80070422是因为关闭了windows update这个服务导致的 这个错误:0x80073D0A是因为关闭了windows firewall这个服务导致的 具体操作: cmd下se ...
- sp_addlinkedserver创建远程服务器查询
远程服务器查询可以分两步完成: 1.建立连接服务器 exec sp_addlinkedserver @server='Test_Server', --被访问的服务器别名 @srvproduct='', ...