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 ...
随机推荐
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 – 员工离职管理
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 – 员工离职管理 当公司有几万人,上千家加盟网点,几个庞大的直属分公司后,系统账户的有效管理也是一个头疼的问题,把所有的帐户及时进行科学 ...
- mysql优化理解笔记(持续更新)
主要包括存储引擎.索引.sql语句 一.存储引擎 目前最常见的是InnoDB和MyISAM两个存储引擎 (1)InnoDB:支持事务处理,提供行级锁.外键约束索引,行锁 (2)MyISAM:支持全文搜 ...
- [JSON].value( keyPath )
语法:[JSON].value( keyPath ) 返回:[String | Null] 说明:获取指定键名路径值的字符串格式 示例: Set jsonObj = toJson("{bod ...
- 【CSV数据文件】
文件参数化设置方法
- 90 [LeetCode] Subsets2
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 持续集成之TeamCity 配置
xcopy /S /Y CodeFirstDemo\CodefirstDemo.Web D:\publish\welcome\Web
- Java抽象与接口的区别
Java抽象与接口的区别 答案方式一.简单来说,1.接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 2.另外,实现接口的一定要实现接口里定义的所有 ...
- VBA基础之Excel 工作薄(Book)的操作(三)
三. Excel 工作薄(Book)的操作1. Excel 创建工作薄(Book) Sub addWorkbook() Workbooks.Add End Sub 2. Excel 打开工作薄(Boo ...
- vue.js学习之 跨域请求代理与axios传参
vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...
- Python 字符串与基本语句
Python特点 python中没有变量的声明 语句结束后没有分号 严格要求缩进 支持很长很长的大数运算(直接在Idle中输入即可) 用"#"来注释 BIF:Bulit-in fu ...