浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html

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

用树状数组维护区间内元素个数,直接在树状数组上统计即可。

然后就\(TLE\)了…………

在桶上使用分块科技统计元素就能\(A\)了。

时间复杂度:\(O(n\sqrt{n})\)

空间复杂度:\(O(n)\)

代码如下:

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define low(i) ((i)&(-(i))) const int maxn=1e5+5,maxm=1e6+5; int n,m,block;
int num[maxn],bel[maxn],L[320],R[320]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct query {
int l,r,a,b,id,ans; bool operator<(const query &a)const {
if(bel[l]!=bel[a.l])return bel[l]<bel[a.l];
if(bel[l]&1)return r<a.r;return r>a.r;
}
}q[maxm]; struct sqrt_technology {
int sum[320],cnt[maxn]; void add(int pos,int v) {
if(cnt[pos]==0&&v==1)sum[bel[pos]]++;
if(cnt[pos]==1&&v==-1)sum[bel[pos]]--;
cnt[pos]+=v;
} int query(int l,int r) {
int res=0;
if(bel[l]==bel[r]) {
for(int i=l;i<=r;i++)
res+=(cnt[i]!=0);
}
else {
for(int i=l;i<=R[bel[l]];i++)
res+=(cnt[i]!=0);
for(int i=L[bel[r]];i<=r;i++)
res+=(cnt[i]!=0);
for(int i=bel[l]+1;i<bel[r];i++)
res+=sum[i];
}
return res;
}
}T; bool cmp(query a,query b) {return a.id<b.id;} int main() {
n=read(),m=read(),block=sqrt(n);
for(int i=1;i<=n;i++) {
num[i]=read(),bel[i]=(i-1)/block+1;
if(bel[i]!=bel[i-1])R[bel[i-1]]=i-1,L[bel[i]]=i;
}R[bel[n]]=n;
for(int i=1;i<=m;i++) {
q[i].id=i;
q[i].l=read(),q[i].r=read();
q[i].a=read(),q[i].b=read();
}
sort(q+1,q+m+1);
int nowl=1,nowr=0;
for(int i=1;i<=m;i++) {
while(nowl<q[i].l)T.add(num[nowl++],-1);
while(nowl>q[i].l)T.add(num[--nowl],1);
while(nowr<q[i].r)T.add(num[++nowr],1);
while(nowr>q[i].r)T.add(num[nowr--],-1);
q[i].ans=T.query(q[i].a,q[i].b);
}
sort(q+1,q+m+1,cmp);
for(int i=1;i<=m;i++)
printf("%d\n",q[i].ans);
return 0;
}

BZOJ3809:Gty的二逼妹子序列的更多相关文章

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

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

  2. [bzoj3809]Gty的二逼妹子序列_莫队_分块

    Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...

  3. BZOJ3809: Gty的二逼妹子序列

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

  4. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

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

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

  6. 2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)

    传送门 题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数. 看了一眼大家应该都知道要莫队了吧. 然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nl ...

  7. 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; int ...

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

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

  9. 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1072  Solved: 292[Submit][Status][Di ...

  10. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

随机推荐

  1. springboot-项目属性配置

    springboot如何新建一个项目参考博客:https://www.cnblogs.com/junyang/p/8151802.html 在springboot默认生成的配置文件的格式是:appli ...

  2. 【HackerRank】Gem Stones

    Gem Stones John has discovered various rocks. Each rock is composed of various elements, and each el ...

  3. gstreamer——文档/资源/使用

    http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...

  4. js学习笔记2(5章操作方法)

    5.2.5操作 concat();将slice(); 5.2.7位置操作的方法 其他的不赘述 5.5 函数 1.函数其实是对象,每一个函数都是function对象的实例,与其他引用类型一样,都具有属性 ...

  5. Whitewidow:SQL 漏洞自动扫描工具

    Whitewidow 是一个开源的 SQL 漏洞自动扫描器,可用通过文件列表运行,或者从 Google 爬取并发现有潜在漏洞的网站. 这个工具支持自动格式化文件.随机用户代理.IP 地址.服务器信息. ...

  6. CentOS防火墙iptables-config的相关配置参数详解

    默认/etc/sysoncifg/iptables-config的配置内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  7. Netsh 命令详解

    1. help帮助指南 2. 常用命令介绍netsh interface ip show addressnetsh interface ip dumpnetsh interface ip dump & ...

  8. Hadoop学习1(初识hadoop)

    Hadoop生态系统的特点 1)源代码开源 2)社区活跃,参与者多 3)涉及分布式存储和计算的各方面 4)已得到企业界的验证 Hadoop构成 1) 分布式文件系统HDFS(Hadoop Distri ...

  9. centos 验证mysql的安装

    一.验证mysql是否安装 1.whereis mysql:如果安装了mysql就会显示mysql安装的地址 2.which mysql:查看文件的运行地址 3.chkconfig --list my ...

  10. ansible实现发布、回滚功能

    ansible的两篇博客,本来是打算合二为一的,发现只用一篇写,嗯,好鬼长.... 一向秉承简单为美的我于是忍痛割爱,一分为二了 ansible实现升级发布.回滚功能 1.应用场景 在实际生产环境中, ...