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 ...
随机推荐
- kaggle入门--泰坦尼克号之灾(手把手教你)
作者:炼己者 具体操作请看这里-- https://www.jianshu.com/p/e79a8c41cb1a 大家也可以看PDF版,用jupyter notebook写的,视觉效果上感觉会更棒 链 ...
- lunix安装
https://www.cnblogs.com/wcwen1990/p/7630545.html
- 第三十九篇 Python异常处理
一. 什么是异常 异常就是程序运行时发生的错误,在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止,在python中,错误触发的异常如下 错误分成两种: #语法 ...
- Linearize an sRGB texture in Photoshop
From:https://forum.unity.com/threads/bug-with-bypass-srgb-sampling.282469/
- JavaScript 数组操作方法 和 ES5数组拓展
JavaScript中数组有各种操作方法,以下通过举例来说明各种方法的使用: 数组操作方法 push 在数组最后添加一个元素 var arr=[3,4,5,6] console.log(arr) // ...
- wamp下安装https 实现 ssl 协议,主要是编写小程序通讯
也不知道腾讯为啥要这个限制,是想卖他的服务器资源么 简单几句话 1 wamp3.0.X的版本不行,我折腾了一天半,放弃了,换成wamp2.5 一次通过 2 证书 去腾讯云申请,单独域名的可以申请免费的 ...
- HADOOP docker(二):HDFS 高可用原理
1.环境简述2.QJM HA简述2.1为什么要做HDFS HA?2.2 HDFS HA的方式2.2 HSFS HA的结构2.3 机器要求3.部署HDFS HA3.1 详细配置3.2 部署HDF ...
- Java学习个人备忘录之线程间的通信
线程间通讯多个线程在处理同一资源,但是任务却不同. class Resource { String name; String sex; } //输入 class Input implements Ru ...
- Java学习个人备忘录之继承
继承的好处1. 提高了代码的复用性.2. 让类与类之间产生了关系,给第三个特征多态提供了前提. java中支持单继承,不直接支持多继承,但对C++中的多继承机制进行改良.java支持多层继承. C继承 ...
- MyBatis 基本构成与框架搭建
核心组件 SqlSessionFactoryBuilder (构造器) 根据配置信息(eg:mybatis-config.xml)或者代码来生成SqlSessionFactory. SqlSessio ...