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]作业——题解的更多相关文章

  1. [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业

    [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj   bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...

  2. 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 &amp; 3236 [Ahoi2013] 作业 题解

    [原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 978  Solved: 476 Descri ...

  3. BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】

    题目链接 BZOJ3236 题解 没想到这题真的是如此暴力 #include<algorithm> #include<iostream> #include<cstring ...

  4. [BZOJ3236]:[Ahoi2013]作业(莫队+分块)

    题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...

  5. [BZOJ3236][AHOI2013]作业:树套树/莫队+分块

    分析 第一问随便搞,直接说第二问. 令原数列为\(seq\),\(pre_i\)为\(seq_i\)这个值上一个出现的位置,于是可以简化询问条件为: \(l \leq i \leq r\) \(a \ ...

  6. bzoj3809 Gty的二逼妹子序列 & bzoj3236 [Ahoi2013]作业 莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3809 https://lydsy.com/JudgeOnline/problem.php?id ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 图片文件转换成Base64编码实现ajax提交图片

    //上传头像图片 function uploadHead(imgPath) { console.log("imgPath = " + imgPath); var image = n ...

  2. nodejs 实现套接字服务

    nodejs实现套接字服务     一 什么是套接字 1.套接字允许一个进程他通过一个IP地址和端口与另一个进程通信,当你实现对运行在同一台服务器上的两个不同进程的进程间通信或访问一个完全不同的服务器 ...

  3. 使用ListView+ObjectDataSource+DataPager实现增删改查加分页

    一.配置objectDataSource 选择业务逻辑层的类 二.配置Select对应的方法,必须是一个带两个整型参数的方法,第一个参数表示要查看的第一条记录的前一条30,第二个参数每页最多能显示的记 ...

  4. 【独家】K8S漏洞报告 | 近期bug fix解读&1.9.11主要bug fix汇总

    *内容提要: 1. Kube-proxy长连接优雅断开机制及IPVS模式实现 2. 10/29--11/19 bug fix汇总分析 3. 1.9.11重要bug fix汇总 在本周的跟踪分析中,以1 ...

  5. 第三模块:面向对象&网络编程基础 第2章 网络编程

    01-计算机基础 02-什么是网络 03-五层协议详解 04-传输层详解 05-什么是Socket 06-基于socket实现简单套接字通信 07-在简单套接字基础上加上通信循环 08-客户端与服务端 ...

  6. VMware 15.0下载及安装教程

    虚拟机 VMware WorkStation Pro15 下载及安装详细解 9虚拟机 VMware WorkStation Pro15 下载及安装详细解. 虚拟机官方网站: https://www.v ...

  7. flask源码走读

    Flask-Origin 源码版本 一直想好好理一下flask的实现,这个项目有Flask 0.1版本源码并加了注解,挺清晰明了的,我在其基础上完成了对Werkzeug的理解部分,大家如果想深入学习的 ...

  8. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  9. synchronized 详细解说

    转自  http://blog.csdn.net/javazejian/article/details/72828483 出自[zejian的博客] 写的很详细很好,做下记录 本篇主要是对Java并发 ...

  10. POJ 1113 Wall(计算几何の凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...