2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)
传送门
题意:多组询问,问区间[l,r]中权值在[a,b]间的数的种类数。
看了一眼大家应该都知道要莫队了吧。
然后很容易想到用树状数组优化修改和查询做到O(mnlogamax)O(m\sqrt nlog_{a_{max}})O(mnlogamax)的时间复杂度。
然后发现可以上一波权值分块,这样的话可以平衡结合降低时间复杂度到O(mn+mamax)O(m\sqrt n+m\sqrt {a_{max}})O(mn+mamax)
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
const int N=1e5+5,M=1e6+5;
int n,m,sum[N][2],cnt[N],a[N],ans[M],sqr1,sqr2,mx=0;
struct Node{int l,r,a,b,id;}qry[M];
inline int idx(int p,int blo){return (p-1)/blo+1;}
inline bool cmp(const Node&a,const Node&b){
int ia=idx(a.l,sqr1),ib=idx(b.l,sqr1);
return ia==ib?a.r<b.r:ia<ib;
}
inline void add(int p){
int val=a[p],blo=idx(val,sqr2);
++sum[blo][0],++cnt[a[p]];
if(cnt[a[p]]==1)++sum[blo][1];
}
inline void del(int p){
int val=a[p],blo=idx(val,sqr2);
--sum[blo][0],--cnt[a[p]];
if(!cnt[a[p]])--sum[blo][1];
}
inline int query(int val){
int blo=idx(val,sqr2),ret=0;
for(ri i=blo-1;i;--i)ret+=sum[i][1];
for(ri i=(blo-1)*sqr2+1;i<=val;++i)ret+=cnt[i]?1:0;
return ret;
}
int main(){
freopen("lx.in","r",stdin);
n=read(),m=read();
for(ri i=1;i<=n;++i)a[i]=read(),mx=max(mx,a[i]);
sqr1=sqrt(n+0.5),sqr2=sqrt(mx+0.5);
for(ri i=1;i<=m;++i)qry[i].l=read(),qry[i].r=read(),qry[i].a=read(),qry[i].b=read(),qry[i].id=i;
sort(qry+1,qry+m+1,cmp);
int pl=1,pr=0;
for(ri tt=1;tt<=m;++tt){
while(pl<qry[tt].l)del(pl++);
while(pl>qry[tt].l)add(--pl);
while(pr<qry[tt].r)add(++pr);
while(pr>qry[tt].r)del(pr--);
ans[qry[tt].id]=query(qry[tt].b)-query(qry[tt].a-1);
}
for(ri i=1;i<=m;++i)cout<<ans[i]<<'\n';
return 0;
}
2019.01.08 bzoj3809: Gty的二逼妹子序列(莫队+权值分块)的更多相关文章
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- [bzoj3809]Gty的二逼妹子序列_莫队_分块
Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...
随机推荐
- Asp.net中GridView使用详解(很全,很经典 转来的)
Asp.net中GridView使用详解 效果图参考:http://hi.baidu.com/hello%5Fworld%5Fws/album/asp%2Enet中以gv开头的图片 l ...
- Maximum GCD(fgets读入)
Maximum GCD https://vjudge.net/contest/288520#problem/V Given the N integers, you have to find the m ...
- 转)VCSA 6.5重启无法访问,报错“503 Service Unavailable”的解决方法
1. 问题 重启vcenter,登陆vsphere client,提示 “503 Service Unavailable (Failed to connect to endpoint: [N7Vmac ...
- 【Linux 线程】常用线程函数复习《四》
1.线程属性的设置 /************************************************************************* > File Name: ...
- java爬取网页Unicode转UTF-8中文
unicode编码简而言之就是将每一个字符用16位2进制数标识.但是通常都用4位的16进制数标识. 例如: 1)中文字符串"你好"的unicode码为:\u60\u597d; 2) ...
- 本地推送UILocalNotification的一些简单方法
1.添加本地推送,需要在app添加推送.可以根据通知的userInfo的不同的键值对来区分不同的通知 UILocalNotification *notification = [[UILocalNoti ...
- centos 6.5 上安装 nvm
NVM 重磅推出NVM curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash Clos ...
- 标准IO缓冲机制
参考资料: https://q16964777.iteye.com/blog/2228244 知道缓冲有几种模式:无缓冲.行缓冲.全缓冲.通过判断FILTE中的 _flags 的判断可以知道究竟是那种 ...
- msdn
https://docs.microsoft.com/en-us/cpp/standard-library/basic-string-class?view=vs-2017
- rabbitmq安装.教程
https://www.cnblogs.com/ericli-ericli/p/5902270.html (rabbitmq安装)https://www.cnblogs.com/iiwen/p/538 ...