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. Vue框架核心之数据劫持

    本文来自网易云社区. 前瞻 当前前端界空前繁荣,各种框架横空出世,包括各类mvvm框架横行霸道,比如Angular.Regular.Vue.React等等,它们最大的优点就是可以实现数据绑定,再也不需 ...

  2. Python3.5+selenium(11)脚本模块化&参数化

    mail126.py脚本如下 from selenium import webdriver from time import sleep from model1 import Login driver ...

  3. uiautomatorviewer定位App元素

    这个工具是Android SDK自带的, 日常的工作中经常要使用的, 在C:\Android\sdk\tools\bin目录下: 双击之, 请注意, 我一般选择第一个机器人小图标Device Scre ...

  4. Python字符串操作大全(非常全!!!)

    1. python编程里字符串的内置方法(非常全) capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居 ...

  5. 浅谈如何写出一个让(坑)人(王)很(之)难(王)发现的bug

    该文章内容来自脚本之家,原文链接:https://www.jb51.net/news/598404.html 程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因 ...

  6. Python爬虫模拟登录带验证码网站

    问题分析: 1.爬取网站时经常会遇到需要登录的问题,这是就需要用到模拟登录的相关方法.python提供了强大的url库,想做到这个并不难.这里以登录学校教务系统为例,做一个简单的例子. 2.首先得明白 ...

  7. java字符转义

    之前对java字符转义这一块稍作了解,在这里理理自己主观浅显的理解 这里会谈谈字符编码的是另一种问题和转义没有关系 以下面代码做分析 System.out.println("a". ...

  8. 给eclipse安装color-theme插件

    给eclipse安装color-theme插件 2016年03月22日 19:16:01 ming_love 阅读数:5193 标签: Eclipse Color Theme 更多 个人分类: jav ...

  9. commons-lang源码解析之StringUtils

    apache的commons工具包是平时使用最多的工具包之一,对其实现方式需要具体了解.commons-lang version 3.1 empty和blank的区别 StringUtils中判断St ...

  10. UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言

    关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...