莫队算法初识~~CodeForces - 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.
题目链接
http://codeforces.com/problemset/problem/617/E
题意
给你一段长度为n的区间
有m次询问
和一个要求的数K
m次查询区间L~R内有多少对 i j 满足 ai^ai+1......^aj =k;
#include<bits/stdc++.h>
using namespace std;
const int maxn = <<;
/*
莫队算法:
只有查询没有修改的操作
O(1)查询
n^1.5 */
struct node{
int l,r,id;
// 左 右 第几个询问
}Q[maxn]; int pos[maxn];
long long ans[maxn];
long long flag[maxn]; //每个前缀出现的次数
int a[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; //第i次查询的结果
}
for(int i=;i<=m;i++){
cout<<ans[i]<<endl;
} }
莫队算法初识~~CodeForces - 617E的更多相关文章
- Codeforces 617E:XOR and Favorite Number(莫队算法)
http://codeforces.com/problemset/problem/617/E 题意:给出n个数,q个询问区间,问这个区间里面有多少个区间[i,j]可以使得ai^ai+1^...^aj ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
- 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 莫队算法
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 —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- codeforces 617 E. XOR and Favorite Number(莫队算法)
题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...
- [莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II
题目大意:给出一个长度为n的数列a. 对于一个询问lj和rj.将a[lj]到a[rj]从小到大排序后并去重.设得到的新数列为b,长度为k,求F1*b1+F2*b2+F3*b3+...+Fk*bk.当中 ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
随机推荐
- Liunx 配置sshd服务
简介 SSH(Secure Shell)是一种能够提供安全远程登录会话的协议,也是目前远程管理Linux系统最首选的方式,因为传统的ftp或telnet服务是不安全的,它们会把帐号口令和数据资料等数据 ...
- 搭建MQTT代理服务器
# 解压tar zxfv mosquitto-1.4.14.tar.gz# 进入目录cd mosquitto-1.4.14# 编译make# 安装sudo make instal 1 启动代理服务在第 ...
- scrapy--boss直聘
Hi,大家好.有段时间没来更新scrapy爬取实例信息了,前2天同事说爬取拉勾,boss直聘等网站信息比较困难.昨天下午开始着手爬取boss直聘内Python爬虫的信息,比想象中的简单很多. 需要解决 ...
- ECSHOP快递物流单号查询插件
本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...
- C语言实例解析精粹学习笔记——29
题目: 将字符行内单字之间的空格平均分配插入到单字之间,以实现字符行排版.也就是输入一个英文句子,单词之间的空格数目不同,将这些空格数平均分配到单词之间,重新输出. 代码如下(是原书中配套的代码,只是 ...
- [BZOJ2120]数颜色(莫队算法)
Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜 ...
- 笔记-python-redis接口
笔记-python-redis接口 1. python 与redis接口 redis是redis数据库的python接口包,为python提供的redis的调用接口. 注:文档内容主要基于h ...
- PHP.22-Smart模版
Smart模版 smarty是一个基于PHP开发的PHP模板引擎.它提供了逻辑与外在内容的分离,简单的讲,目的就是要使PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美 ...
- Web性能测试问题,mysql分析之一
在做性能测试执行中,发现性能很慢,顺藤摸瓜找一下是什么问题? 并发40个用XXX场景 1.运行过程中监控mysql数据库的CPU过高,达到65%: %CPU %Mem %Disk %Net ...
- 跟着学!教你开发第一个Java程序
今天我们的目标是开发人生中的第一个Java程序,虽然可能会很简单,但是这小小的一步却是跨入IT行业的一大步!下面我们来一起来仔细的了解开发的流程. 准备工作 1,作为一名准程序猿自备一台电脑那是必不可 ...