CF 617E【莫队求区间异或和】
4 seconds
256 megabytes
standard input
standard output
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.
The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.
The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.
Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.
Print m lines, answer the queries in the order they appear in the input.
6 2 3
1 2 1 1 0 3
1 6
3 5
7
0
5 3 1
1 1 1 1 1
1 5
2 4
1 3
9
4
4
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length.
题目大意:
给定长度为n的序列,m个询问和常数K
多个询问L,R,求有多少对(i,j)满足L<=i<=j<=R且i到j异或和为K
分析:
这题妙啊
总结经验
1.看到多个询问L,R的要考虑到莫队
2.看到区间异或和的要想到转化成前缀和,变成Si^Sj==K的简化问题
3.看到异或的时候要考虑到,经过异或权值范围会比数据中给得大(异或一下权值就不是原范围了)
3.看到权值不是很大的情况下,考虑能不能用桶
4.看到求多少对的问题要注意开long long
5.区间异或和转换成前缀和后,实际取值中i为L-1<=i<R,要特判加上L-1的贡献(就类似于树上莫队特判lca)
转载自acha
2017-04-06
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=4e5+5;
typedef long long ll;
struct block{int l,r,id;}Q[N];
int n,m,K,bsize,a[N];
ll nowans,ans[N],cnt[N*10];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool operator <(const block &a,const block &b){
return a.l/bsize!=b.l/bsize?a.l/bsize<b.l/bsize:a.r<b.r;
}
inline void ins(int x){
nowans+=cnt[K^x];
cnt[x]++;
}
inline void del(int x){
cnt[x]--;
nowans-=cnt[K^x];
}
int main(){
n=read();m=read();K=read();bsize=sqrt(n+0.5);
for(int i=1;i<=n;i++) a[i]=read(),a[i]^=a[i-1];
for(int i=1;i<=m;i++) Q[i].l=read(),Q[i].r=read(),Q[i].id=i;
sort(Q+1,Q+m+1);
int l=1,r=0;
for(int i=1;i<=m;i++){
while(l>Q[i].l) ins(a[--l]);
while(l<Q[i].l) del(a[l++]);
while(r<Q[i].r) ins(a[++r]);
while(r>Q[i].r) del(a[r--]);
ans[Q[i].id]=nowans+cnt[K^a[l-1]];
}
for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
#define EF if(ch==EOF) return EOF;
#define EX if(ch==EOF) return x*f;
using namespace std;
const int N=4e5+;
struct node{int l,r,id;}q[N];
int n,m,k,bsize,a[N];
long long nowans,ans[N],cnt[N*];
inline int read(){
int x=,f=;char ch=getchar();EF
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();EF}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();EX}
return x*f;
}
inline bool cmp(const node &a,const node &b){
return a.l/bsize!=b.l/bsize?a.l/bsize<b.l/bsize:a.r<b.r;
}
inline void ins(int x){
nowans+=cnt[k^x];
cnt[x]++;
}
inline void del(int x){
cnt[x]--;
nowans-=cnt[k^x];
}
int main(){
n=read();m=read();k=read();
for(int i=;i<=n;i++) a[i]=read(),a[i]^=a[i-];
for(int i=;i<=m;i++) q[i].l=read(),q[i].r=read(),q[i].id=i;
bsize=sqrt(n);
sort(q+,q+m+,cmp);
int l=q[].l,r=q[].r;
for(int i=l;i<=r;i++) ins(a[i]);
ans[q[].id]=nowans+cnt[k^a[l-]];
for(int i=;i<=m;i++){
while(q[i].l<l) ins(a[--l]);
while(q[i].r>r) ins(a[++r]);
while(q[i].l>l) del(a[l++]);
while(q[i].r<r) del(a[r--]);
ans[q[i].id]=nowans+cnt[k^a[l-]];
}
for(int i=;i<=m;i++) printf("%I64d\n",ans[i]);
return ;
}
referance:
http://codeforces.com/blog/entry/22971(官方解题报告)
http://blog.csdn.net/qq978874169/article/details/51241737(莫队算法入门)
CF 617E【莫队求区间异或和】的更多相关文章
- CF 86D 莫队(卡常数)
CF 86D 莫队(卡常数) D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes i ...
- Codeforces617E【莫队算法+前缀异或】
题意: 给出一系列数,对每个查询区间,计算有多少个子区间异或为k. 思路: 可以先预处理异或前缀,一个区间[L,R]的异或值=sum[R]^sum[L-1]; 如果当前区间是[a,b],加一个右端点b ...
- HDU5381【莫队算法+区间GCD特性】
前言: 主要最近在刷莫队的题,这题GCD的特性让我对莫队的使用也有了新的想法.给福利:神犇的一套莫队算法题 先撇开题目,光说裸的一个莫队算法,主要的复杂度就是n*sqrt(n)对吧,这里我忽略了一个左 ...
- XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和
CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! = j) ...
- 浙工大新生赛莫队处理+区间DP+KMP+分析题
题目描述 读入一个长度为n的整数数列a1,a2,…,an,以及一个整数K. q组询问. 每组询问包含一个二元组(l, r), 其中1≤l≤r≤ n, 求所有满足以下条件的二元组(l2, r2)的数目: ...
- hdoj6483 A Sequence Game(ST预处理RMQ+莫队)
传送:http://acm.hdu.edu.cn/showproblem.php?pid=6483 题意:有长度为$n$的数组,对于一个子区间$[l,r]$内,存在最大值$mx$与最小值$mi$,有$ ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
题目链接: BZOJ - 3236 BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...
随机推荐
- hibernate学习系列-----(6)hibernate对集合属性的操作之Set集合篇
先说一段废话吧,本打算每天把所学的知识总结为博客的,但是昨天为什么没有写呢?没有学习吗?No,那是为什么?贪玩,对,这位同学说对了,老实说昨天感觉身体不怎么舒服,大家都知道,这其实就是为自己懒找借口, ...
- 野哥点评了Facebook、Amazon、Google、微软和苹果
娱乐一下.我们来比較一下4家相似的科技公司的软件政治观. (1)Facebook-诊断:极端自由. Facebook的规模已经非常大了.可他们的行为处事仍然像是一家创业公司.并且到眼下为止似乎也活得挺 ...
- Django——django1.6 基于类的通用视图
最初 django 的视图都是用函数实现的,后来开发出一些通用视图函数,以取代某些常见的重复性代码.通用视图就像是一些封装好的处理器,使用它们的时候只须要给出特定的参数集即可,不必关心具体的实现.各种 ...
- 【Python】八大排序算法的比较
排序是数据处理比较核心的操作,八大排序算法分别是:直接插入排序.希尔排序.简单选择排序.堆排序.冒泡排序.快速排序.归并排序.基数排序 以下是排序图解: 直接插入排序 思想 直接插入排序是一种最简单的 ...
- Android自定义圆形进度条,完成类似LOFTER效果
1.http://stackoverflow.com/questions/3760381/rotating-image-animation-list-or-animated-rotate-androi ...
- Android Shape 详解
1 http://blog.csdn.net/feng88724/article/details/6398193 2 <shape xmlns:android="http://sche ...
- 解决:System.Data.SqlClient.SqlError: FILESTREAM 功能被禁用
还原 AdventureWorks Sample DataBase 时遇到 FILESTREAM feature is disabled 错误提示. FileStream是SQL Server 200 ...
- ant用途及简单实现
ant用途及简单实现 标签: antjavadeletejarbuildjavaee 2012-07-17 14:15 5945人阅读 评论(0) 收藏 举报 分类: other(6) Ant工具 ...
- Backup and Recovery Basics1
一.Backup and Recovery Overview 1.Backup and Recovery Overview 1.1 What is Backup and Recovery? 一般,备份 ...
- 摄像头驱动_摄像头驱动程序必需的11个ioctl及摄像头数据的获取过程
摄像头驱动_摄像头驱动程序必需的11个ioctl及摄像头数据的获取过程 根据虚拟驱动vivi的使用过程彻底分析摄像头驱动// 1~2都是在v4l2_open里调用1. open2. ioctl(4, ...