题意

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

\(n\leq 10^5,m\leq m\leq 10^6, a\leq b\leq n\)。

分析

  • 直接莫队+树状数组的复杂度是 \(O(m\sqrt n\ logn)\)。

  • 把树状数组改成分块,这样查询的时间是 \(O(\sqrt n)\) ,但是修改是 \(O(1)\) 的。

  • 总时间复杂度为 \(O(m\sqrt n)\)。

当两种操作时间复杂度不平衡时或许有方法调节以降低复杂度

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].lst,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int M=1e6 + 7,N=1e5 + 7;
int n,sz,m;
int bl[N],cnt[N],a[N],num[N];
int s[N],ans[M];
struct qs{
int l,r,a,b,id;
bool operator <(const qs &rhs)const{
if(l/sz!=rhs.l/sz) return l/sz<rhs.l/sz;
return r<rhs.r;
}
}q[M];
int L(int x){return (x-1)*sz+1;}
int R(int x){return x*sz;}
void calc(int p,int f){
if(f==1&&++cnt[ s[p] ]==1) {
a[ s[p] ]+=f;
num[ bl[ s[p] ] ]+=f;
}
if(f==-1&&--cnt[ s[p] ]==0){
a[ s[p] ]+=f;
num[ bl[ s[p] ] ]+=f;
}
}
int query(int l,int r){
int ans=0;
if(bl[l]==bl[r]){
for(int i=l;i<=r;++i) ans+=a[i];
return ans;
}
for(int b=bl[l]+1;b<bl[r];++b) ans+=num[b];
for(int i=l;i<=R( bl[l] );++i) ans+=a[i];
for(int i=L( bl[r] );i<=r;++i) ans+=a[i];
return ans;
}
int main(){
n=gi(),m=gi(),sz=sqrt(n);
rep(i,1,n) s[i]=gi();
rep(i,1,1e5) bl[i]=(i-1)/sz+1;
rep(i,1,m) q[i].l=gi(),q[i].r=gi(),q[i].a=gi(),q[i].b=gi(),q[i].id=i;
sort(q+1,q+1+m);
int L=1,R=0;
rep(i,1,m){
while(R<q[i].r) calc(++R,1);
while(L>q[i].l) calc(--L,1);
while(R>q[i].r) calc(R--,-1);
while(L<q[i].l) calc(L++,-1);
ans[q[i].id]=query(q[i].a,q[i].b);
}
rep(i,1,m) printf("%d\n",ans[i]);
return 0;
}

[BZOJ3809]Gty的二逼妹子序列[莫队+分块]的更多相关文章

  1. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  2. 【BZOJ3809】Gty的二逼妹子序列 莫队 分块

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...

  3. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

  4. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  5. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

  6. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

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

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

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

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

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

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

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

随机推荐

  1. 2016年度最受欢迎的100个 Java 库

    [编者按]本文作者为 Henn Idan,主要介绍基于 GitHub 中的数据分析,得出的2016年度最受欢迎的100个 Java 库.本文系国内 ITOM 管理平台 OneAPM 编译呈现. 谁拔得 ...

  2. .NET Dispose模式的实现

    以下是代码: /// <summary> /// Dispose Pattern /// </summary> /// <remarks> /// 由逻辑可知: / ...

  3. 高通 display 驱动【转】

    高通display驱动 0. 关键字 MDSS : 高通平台lcd multimedia Display sub system DSI: Display Serial Interface qcom,m ...

  4. Configure network bonding on RHEL (Red Hat Enterprise Linux)

    Question: Recently I have to use the RHEL and need to config the network with a few NICs. Here comes ...

  5. wc 统计文件的行数,字数,字符

    格式:wc 参数 文件  默认统计文件的行数,字数,字符. -l   统计有多少行数 -c   统计有多少个字节 -m  统计有多少个字符 -w 统计有多少个字数

  6. JDK5 新特性之 可变参数的方法(2)---asList

    > Arrays.asList(T - a)方法的使用 >UnsupportedOperationException分析     Arrays.asList(T - a)方法的使用 pac ...

  7. VS2008 开发wince程序设备调试

    今天之前开发的一个wince程序,用户反馈报错,由于很久没玩了,从用户那里拿来设备.结果怎么调试的忘记了.在网上找了些资料,自己有摸索了一下.才搞定. 1.安装Microsoft ActiveSync ...

  8. 启动 uiautomatorviewer 时报 SWT folder '..\lib\location of your Java installation.' does not exist.

    现象,之前本机上的 uiautomatorviewer 一直是好的,最近这段时间无故就不行了,报如标题错误,网上找了各种办法仍无法有效解决,静心细想上一次使用该工具时到目前对本机有做什么跟系统或者工具 ...

  9. APP性能测试,网易Emmagee工具

    APK地址:https://github.com/NetEase/Emmagee/releases/download/V1.2.1/Emmagee-1.2.1.apk 开源代码github地址:htt ...

  10. js判断360浏览器 兼容模式IE版本

    var ms_ie = false; var ua = window.navigator.userAgent.toLowerCase(); var old_ie = ua.indexOf('MSIE' ...