[AHOI2013]作业

题目大意:

给定一个长度为\(n(n\le10^5)\)的数列\(A(1\le A_i\le n)\)。\(m(m\le10^6)\)次询问,每次询问区间\([l,r]\)内满足\(a\le A_i\le b\)的数有多少,去重后又有多少。

思路:

莫队+分块。

时间复杂度\(\mathcal O(n\sqrt n)\)。

源代码:

#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,M=1e6;
int n,m,block,a[N],ans0[M],ans1[M],cnt[N],sum[N],tot[N],bel[N],beg[N],end[N];
struct Query {
int l,r,a,b,id;
bool operator < (const Query &rhs) const {
if(l/block==rhs.l/block) return r<rhs.r;
return l/block<rhs.l/block;
}
};
Query q[M];
inline void ins(const int &x) {
if(!cnt[x]++) tot[bel[x]]++;
sum[bel[x]]++;
}
inline void del(const int &x) {
if(!--cnt[x]) tot[bel[x]]--;
sum[bel[x]]--;
}
inline std::pair<int,int> query(const int &l,const int &r) {
int ret0=0,ret1=0;
if(bel[l]==bel[r]) {
for(register int i=l;i<=r;i++) {
ret0+=cnt[i];
ret1+=!!cnt[i];
}
return std::make_pair(ret0,ret1);
}
for(register int i=l;i<=end[bel[l]];i++) {
ret0+=cnt[i];
ret1+=!!cnt[i];
}
for(register int i=bel[l]+1;i<bel[r];i++) {
ret0+=sum[i];
ret1+=tot[i];
}
for(register int i=beg[bel[r]];i<=r;i++) {
ret0+=cnt[i];
ret1+=!!cnt[i];
}
return std::make_pair(ret0,ret1);
}
int main() {
n=getint(),m=getint(),block=sqrt(n);
for(register int i=1;i<=n;i++) a[i]=getint();
for(register int i=0;i<m;i++) {
q[i].l=getint();
q[i].r=getint();
q[i].a=getint();
q[i].b=getint();
q[i].id=i;
}
std::sort(&q[0],&q[m]);
for(register int i=1;i<=n;i++) {
bel[i]=i/block;
if(!beg[bel[i]]) beg[bel[i]]=i;
end[bel[i]]=i;
}
for(register int i=0,l=1,r=0;i<m;i++) {
while(r<q[i].r) ins(a[++r]);
while(r>q[i].r) del(a[r--]);
while(l>q[i].l) ins(a[--l]);
while(l<q[i].l) del(a[l++]);
const std::pair<int,int> p=query(q[i].a,q[i].b);
ans0[q[i].id]=p.first;
ans1[q[i].id]=p.second;
}
for(register int i=0;i<m;i++) {
printf("%d %d\n",ans0[i],ans1[i]);
}
return 0;
}

[AHOI2013]作业的更多相关文章

  1. BZOJ 3236: [Ahoi2013]作业

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1393  Solved: 562[Submit][Status ...

  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. Bzoj 3236: [Ahoi2013]作业 莫队,分块

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1113  Solved: 428[Submit][Status ...

  4. BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )

    莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢... ----------------------------------------------- ...

  5. BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块

    BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块 Description Autumn和Bakser又在研究Gty的妹子序列了 ...

  6. 【Luogu4396】[AHOI2013]作业(莫队)

    [Luogu4396][AHOI2013]作业(莫队) 题面 洛谷 题解 模板题 #include<iostream> #include<cstdio> #include< ...

  7. 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块

    [BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...

  8. BZOJ3236: [AHOI2013]作业

    BZOJ3236: [AHOI2013]作业 题目描述 传送门 行,我知道是Please contact lydsy2012@163.com! 传送门2 题目分析 这题两问还是非常,emmmm. 首先 ...

  9. bzoj 3236: [Ahoi2013]作业(缺线段树)

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1744  Solved: 702[Submit][Status ...

随机推荐

  1. Lucene6.6.0 案例与学习路线

    之前在学习Lucene这个全文检索工具,为项目搜索引擎的开发打下基础.在这里先分享一下关于Lucene的学习心得. 核心的学习流程是:索引文件格式--索引创建过程--检索流程. 1.首先建议参看这篇精 ...

  2. 何凯文每日一句打卡||DAY7

  3. assign()函数

    tf中assign()函数可用于对变量进行更新包括变量的value和shape. 涉及以下函数: tf.assign(ref, value, validate_shape = None, use_lo ...

  4. 第9月第26天 pairs和ipairs cocos2dx 动画

    1. a={ ip = "127.0.0.1", port = 6789 } for i,v in pairs(a) do print(i,v) end a={1} for i,v ...

  5. 修改input placeholder样式

    <style> /* 通用 */ ::-webkit-input-placeholder { color: rgb(235, 126, 107); } ::-moz-placeholder ...

  6. MongoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MongoDB - 4

    我们在之前的 MongoDB 之 手把手教你增删改查 MongoDB - 2 中提到过 $set 这个系统关键字,用来修改值的对吧 但是MongoDB中类似这样的关键字有很多, $lt $gt $lt ...

  7. 记关于vue-cli3 本地代理模拟数据的实践

    网上说的基本都是使用express或http-server作为服务器或其它什么东西自己把玩php也有些年头,就用php好了 服务环境 apache,php先配置好隐藏php后缀扩展名: 在httpd. ...

  8. arm GIC介绍之一【转】

    转自:https://blog.csdn.net/sunsissy/article/details/73791470 GIC是ARM架构中及其重要的部分,本文只在公开ARM对应资料基础上,以MTK开发 ...

  9. DM816X 实现 USB HID Gadget 鼠标键盘功能【转】

    转自:https://my.oschina.net/renyongke/blog/410695 开发环境: 平台: DM8168 内核 :linux 2.6.32 RDK:DVRRDK_04.00.0 ...

  10. 004_加速国内docker源下载速度

    docker下载慢的不行.国内加速器地址 http://355dbe53.m.daocloud.iohttps://docker.mirrors.ustc.edu.cn https://hub-mir ...