[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. 解决linux mysql命令 bash: mysql: command not found 的方法

    错误: root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这 ...

  2. H5 以及 CSS3

    <!DOCTYPE html> <html> <head> <style> *{ padding:0; margin:0; } header{ disp ...

  3. sh脚本学习之:变量

    变量的创建 环境配置 /etc/profile =>~/.bash_profile(~/.bash_login,~/.profile) => ~/.bashrc sh声明 name=&qu ...

  4. 利用fiddler来模拟低速环境

    为了让我们的站点拥有更好的用户体验,更短的加载时间,我们会“按需加载”页面的资源. 在调试程序的时候,我们希望能有一个低速率的网络环境来模拟真实线上的环境,这个时候fiddler(下载fiddler请 ...

  5. 五行代码终极完美解决从IE6到Chrome所有浏览器的position:fixed;以及闪动问题

    这个方法其实已经使用很久了,之前主要在嵌入式WebQQ等产品中用过,现在拿出来分享一下吧,是目前最简洁的方式来实现ie6的position:fixed; 失效bug,以及的其他方法的闪动问题,CSS代 ...

  6. JS高级前端开发群加群说明

    JS高级前端开发群加群说明 *一.文章背景: *二. 高级群: *三. 加入方式: *四. 说明: 一.文章背景: 去年年初建了几个群,在不经意间火了,一直排在"前端开发"关键字搜 ...

  7. svn使用笔记

    一.checkout:第一次下载trunk里面的代码到本地 二.commit:提交一些修改* out of date : 本地版本号 < 服务器版本号* 如果过期,就update,可能会出现co ...

  8. 如何用Procmon.exe来监视SQLSERVER的logwrite大小

    如何用Procmon.exe来监视SQLSERVER的logwrite大小 在微软亚太区数据库技术支持组官方博客里面,你会发现很多篇文章都用到了Procmon.exe这个工具 今天我也介绍一下这个工具 ...

  9. CentOS7 关闭防火墙和selinux

    本文将简单介绍在CentOS7上如何临时和永久关闭防火墙和selinux. 关闭防火墙 # 查看防火墙状态 [root@localhost ~]# systemctl status firewalld ...

  10. [Alg::DP] 袋鼠过河

    一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...