BZOJ3236:[AHOI2013]作业——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3236

第一种做法:
建两棵主席树分别处理两个问题。
第一个问题水,第二个问题参考SPOJ3267/DQUERY:D-query
但是代码量巨大,显然不能写。
第二种做法:
参考:https://blog.csdn.net/clover_hxy/article/details/56288794
对询问离线莫队,然后莫队里面套值域分块。
……值域分块还是很好写的就不讲了。
可以看出代码量巨短。
(emmm果然数据结构学傻了想的第一种做法敲了十分钟果断弃了查题解。)
(莫队还是太菜了要多练。)
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1e5+;
const int M=1e6+;
inline int read(){
int x=,w=;char ch=;
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*w;
}
struct data{
int pos,l,r,a,b;
}q[M];
int a[N],cnt[N],ans[M][],v[N],num[N],s,n,m;
inline int bel(int x){return (x-)/s+;}
inline bool cmp(data a,data b){
return bel(a.l)==bel(b.l)?a.r<b.r:a.l<b.l;
}
inline void add(int k){
if(!cnt[k])v[bel(k)]++;
cnt[k]++;num[bel(k)]++;
}
inline void del(int k){
cnt[k]--;num[bel(k)]--;
if(!cnt[k])v[bel(k)]--;
}
void query(int pos,int l,int r){
if(bel(l)==bel(r)){
for(int i=l;i<=r;i++){
if(cnt[i])ans[pos][]++,ans[pos][]+=cnt[i];
}
return;
}
for(int i=l;i<=bel(l)*s;i++)
if(cnt[i])ans[pos][]++,ans[pos][]+=cnt[i];
for(int i=(bel(r)-)*s+;i<=r;i++)
if(cnt[i])ans[pos][]++,ans[pos][]+=cnt[i];
for(int i=bel(l)+;i<=bel(r)-;i++)
ans[pos][]+=v[i],ans[pos][]+=num[i];
}
int main(){
n=read(),m=read();
s=sqrt(n);
for(int i=;i<=n;i++)a[i]=read();
for(int i=;i<=m;i++){
q[i].pos=i,q[i].l=read(),q[i].r=read();
q[i].a=read(),q[i].b=read();
}
sort(q+,q+m+,cmp);
int ql=,qr=;
for(int i=;i<=m;i++){
while(ql<q[i].l)del(a[ql++]);
while(ql>q[i].l)add(a[--ql]);
while(qr<q[i].r)add(a[++qr]);
while(qr>q[i].r)del(a[qr--]);
query(q[i].pos,q[i].a,q[i].b);
}
for(int i=;i<=m;i++){
printf("%d %d\n",ans[i][],ans[i][]);
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
BZOJ3236:[AHOI2013]作业——题解的更多相关文章
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 & 3236 [Ahoi2013] 作业 题解
[原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 978 Solved: 476 Descri ...
- BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】
题目链接 BZOJ3236 题解 没想到这题真的是如此暴力 #include<algorithm> #include<iostream> #include<cstring ...
- [BZOJ3236]:[Ahoi2013]作业(莫队+分块)
题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...
- [BZOJ3236][AHOI2013]作业:树套树/莫队+分块
分析 第一问随便搞,直接说第二问. 令原数列为\(seq\),\(pre_i\)为\(seq_i\)这个值上一个出现的位置,于是可以简化询问条件为: \(l \leq i \leq r\) \(a \ ...
- bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...
- BZOJ3236: [Ahoi2013]作业
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- BZOJ3236:[AHOI2013]作业(莫队,分块)
Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...
随机推荐
- MySQL高级-MySQL安装
1.mysql安装 检查系统是否安装过mysql 查询命令:rpm -qa|grep -i mysql 删除命令:rpm -e RPM软件包名(该名字是上一个命令查出来的名字) 安装命令:rpm -i ...
- SpringBoot学习:整合Redis
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...
- Hive支持行级update、delete时遇到的问题
Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务.(行级的insert好像不配置也能运 ...
- 『Python Kivy』API说明:kivy.app.App
App类是创建Kivy应用的基础.我们可以将其看成是Kivy运行循环当中的主入口.在绝大多数的例子中,你创建这个类的子类,然后构建你自己的应用.当你已经准备好开始应用的整个生命周期时,你可以实例化你定 ...
- katalon系列五:使用Katalon Studio手动编写WEB自动化脚本
上一篇主要讲了怎么录制脚本,这次我们看看怎么手动编写脚本,接下来就编写一个简单的用百度搜索的脚本. 1.我们先抓取页面上的元素,点击工具栏上的Spy Web按钮(地球上有个绿点),URL输入百度地址, ...
- 使用flask_limiter设定API配额
前言 闲来无事,突然想到了以前做过的关于后台API安全方面的事,关于接口访问配额的设置,flask有没有很好的库支持呢?一找还真有!主要是对照了库的官方文档自己写了下dome,以供参考. # -*- ...
- *转载 Tarjan有向图详解
注意! 文章转自:https://www.cnblogs.com/liwenchi/p/7259306.html,如有造成任何侵权行为,请与我联系.我会在第一时间删除. 不过说实话,这大佬写的真的强, ...
- 最短路径算法(II)
什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...
- 主题模型 LDA 入门
主题模型 LDA 入门(附 Python 代码) 一.主题模型 在文本挖掘领域,大量的数据都是非结构化的,很难从信息中直接获取相关和期望的信息,一种文本挖掘的方法:主题模型(Topic Model ...
- codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...