Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)
题目链接:http://codeforces.com/contest/617/problem/E
题目大意:有n个数和m次查询,每次查询区间[l, r]问满足ai ^ ai+1 ^ ... ^ aj == k的(i, j) (l <= i <= j <= r)有多少对。
解题思路:先预处理出一个前缀异或和数组sum数组,则a[l]^a[l+1]^a[l+2]……^a[r]就等于sum[r]^sum[l-1]
然后我们采用莫队算法,用一个数组cnt数组记录前缀和出现的次数
我们要找之前有多少个前缀异或和与现在的前缀异或值为k,对应到cnt数组去找a[i]^k的个数,并更新答案就行了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=<<;
ll pos[maxn],flag[maxn],ans[maxn];
int a[maxn];
struct node{
int l,r,id;
}Q[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=;
ll 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-];
pos[i]=i/sz;
}
for(int i=;i<=m;i++){
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;
}
flag[]=;
sort(Q+,Q+m+,cmp);
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 ;
}
Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)的更多相关文章
- 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 ...
- 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 Round #340 (Div. 2) E XOR and Favorite Number 莫队板子
#include<bits/stdc++.h> using namespace std; <<; struct node{ int l,r; int id; }q[N]; in ...
- 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 Round #340 (Div. 2) E. XOR and Favorite Number
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- [Codeforces Round #340 (Div. 2)]
[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...
- Codeforces Round #340 (Div. 2) E 莫队+前缀异或和
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 ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
随机推荐
- 双系统使用Linux引导
今天在装linux的window双系统时,出现在无法使用linux引导的问题,开机总是自动进windows,照理来说我先装的window,后装的linux,应该是开机进grub引导才对.在主板的boo ...
- IE等浏览器兼容问题解决方案
<meta http-equiv="X-UA-Compatible" content="IE=100" /> 在<head>标签中添加.
- docker-dnsmasq使用
docker-dnsmasq支持通过web页面配置域名映射,镜像地址:https://hub.docker.com/r/jpillora/dnsmasq 使用步骤如下: 1.在Docker宿主上创建 ...
- vmware 虚拟 CPU 性能计数器事件与主机 CPU 不兼容。
错误提示:虚拟 CPU 性能计数器事件与主机 CPU 不兼容. 点放弃 如果是暂时状态选关机, 丢失掉运行状态,即可保留硬盘内容重新开机
- linux中的"空白字符"
[参考这个c语言中的空白字符文章] (http://blog.csdn.net/boyinnju/article/details/6877087) 所谓: linux中的"空白字符" ...
- leetcode 374猜数字大小
// Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...
- leetcode 52 N皇后问题 II
51的简化版,省去根据排列话棋盘的工作,直接计数,代码: class Solution { public: int totalNQueens(int n) { ; vector<); dfs(n ...
- 协议:FTP
ylbtech-协议:FTP FTP(File Transfer Protocol,文件传输协议) 是 TCP/IP 协议组中的协议之一.FTP协议包括两个组成部分,其一为FTP服务器,其二为FTP客 ...
- pve-备份
一个50g的磁盘,用了13分钟 INFO: starting new backup job: vzdump 111 --node cu-pve04 --mode snapshot --compress ...
- oracle查询语句,根据中文的拼音排序
SELECT * FROM USER t ORDER BY nlssort(FIRSTNAME, 'NLS_SORT=SCHINESE_PINYIN_M')