Codeforces - 617E 年轻人的第一道莫队
我对莫队算法最为纠结的地方就是区间端点处,应该是像代码里那样理解吧
cnt[i]表示i出现的次数
maxn开2e6比较保险
/*H E A D*/
struct Query{
int l,r,id;
}q[maxn];
int size;
bool cmp(Query a,Query b){
if(a.l/size!=b.l/size) return a.l/size<b.l/size;
else return a.r<b.r;
}
ll cnt[maxn],a[maxn];
ll ans[maxn];
int main(){
int n,m,k;
while(cin>>n>>m>>k){
memset(cnt,0,sizeof cnt);
rep(i,1,n) a[i]=read();
rep(i,2,n) a[i]^=a[i-1];
rep(i,1,m){
q[i].l=read();
q[i].r=read();
q[i].id=i;
}
size=sqrt(n);
sort(q+1,q+1+m,cmp);
int l=1,r=0;//l-1=0 r=0
cnt[0]++;
ll tmp=0;
rep(i,1,m){
while(l<q[i].l){//删去[l,q[i].l-1]
cnt[a[l-1]]--;
tmp-=cnt[a[l-1]^k];
l++;
}
while(l>q[i].l){//添加[q[i].l,l-1]
tmp+=cnt[a[l-2]^k];//l-1-1
cnt[a[l-2]]++;
l--;
}
while(r<q[i].r){//增加[r+1,q[i].r]
tmp+=cnt[a[r+1]^k];
cnt[a[r+1]]++;
r++;
}
while(r>q[i].r){//删去[q[i].r+1,r]
cnt[a[r]]--;
tmp-=cnt[a[r]^k];
r--;
}
ans[q[i].id]=tmp;
}
rep(i,1,m) println(ans[i]);
}
return 0;
}
Codeforces - 617E 年轻人的第一道莫队的更多相关文章
- Codeforces - 617E 年轻人的第一道莫队·改
题意:给出\(n,m,k,a[1...n]\),对于每次询问,求\([l,r]\)中\(a[i] \ xor \ a[i+1] \ xor \ ...a[j],l<=i<=j<=r\ ...
- CodeForces - 617E XOR and Favorite Number 莫队算法
https://vjudge.net/problem/CodeForces-617E 题意,给你n个数ax,m个询问Ly,Ry, 问LR内有几对i,j,使得ai^...^ aj =k. 题解:第一道 ...
- Codeforces 617E XOR and Favorite Number莫队
http://codeforces.com/contest/617/problem/E 题意:给出q个查询,每次询问区间内连续异或值为k的有几种情况. 思路:没有区间修改,而且扩展端点,减小端点在前缀 ...
- XOR and Favorite Number CodeForces - 617E(前缀异或+莫队)
题意原文地址:https://blog.csdn.net/chenzhenyu123456/article/details/50574169 题意:有n个数和m次查询,每次查询区间[l, r]问满足a ...
- codeforces 617E. XOR and Favorite Number 莫队
题目链接 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k. 维护一个前缀异或值就可以了. 要注意的是 区间[l ...
- Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
题目链接:http://codeforces.com/problemset/problem/351/D 题目大意:有n个数,每次可以删除掉数值相同并且所在位置成等差数列的数(只删2个数或者只删1个数应 ...
- 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 - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
随机推荐
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- shell chmod中数字与字母的含义
数字与字母的组合是chmod命令赋予文件,目录访问权限的方式 访问权限:可读,可写,可执行 字母表示:r , w , x 数字表示:4 , 2 , 1 , ...
- Intent对象若干数据项的含义总结
Intent作为组件之间通信的手段和协议,包含了诸如Action.Data.Type.Category.Component.Extras和Flags等数据项,各自拥有各自的含义和作用.当调用组件发出一 ...
- 学习React中遇到的问题
1.执行eject后,再次启动项目报错 情景:使用create-react-app搭建了项目,启动没有问题,然后执行 $ yarn eject 暴露出webpack配置文件等,再次 $ yarn st ...
- 国外物联网平台(8):Telit
国外物联网平台(8) ——Telit 马智 定位 We Bring IoT to Life Telit提供世界上最全面的高性能物联网模块.连接服务和软件. 产品体系 模块 Telit提供丰富专业的物联 ...
- Go语言最佳实践——异常和错误
Go语言将错误和异常两者区分对待. 1.Go语言中处理错误的惯用法是将错误以函数或者方法最后一个返回值的形式将其返回,并总是在调用它的地方检查返回的错误值. 2.对于“不可能发生的事情”称为异常,可使 ...
- 吐槽一下wp toolkit ToggleSwitch控件
之前用法: <toolkit:ToggleSwitch> <toolkit:ToggleSwitch.Header> <TextBlock Text="2323 ...
- C#多线程编程实战1.2暂停线程(休眠)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- angular 辅助路由
- angular启动过程
第一步: .angular-cli.json 第二步: 第三步: 第四步: 第五步: