【bzoj3809】【GTY的二逼妹子序列】【莫队+分块】
来源:互联网 发布:远程网络教育入学考试 编辑:程序博客网 时间:2024/04/29 11:51
Description
Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
为了方便,我们规定妹子们的美丽度全都在[1,n]中。
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),对于m(1<=m<=1000000)次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。
Input
第一行包括两个整数n,m(1<=n<=100000,1<=m<=1000000),表示数列s中的元素数和询问数。
第二行包括n个整数s1...sn(1<=si<=n)。
接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
保证涉及的所有数在C++的int内。
保证输入合法。
Output
对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。
Sample Input
10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4
Sample Output
2
0
0
2
1
1
1
0
1
2
0
0
2
1
1
1
0
1
2
HINT
样例的部分解释:
5 9 1 2
子序列为4 1 5 1 2
在[1,2]里的权值有1,1,2,有2种,因此答案为2。
3 4 7 9
子序列为5 1
在[7,9]里的权值有5,有1种,因此答案为1。
4 4 2 5
子序列为1
没有权值在[2,5]中的,因此答案为0。
2 3 4 7
子序列为4 5
权值在[4,7]中的有4,5,因此答案为2。
建议使用输入/输出优化。
题解:
普通的莫队算法+树状数组会TLE.
我们可以用分块代替树状数组.
考虑将颜色分块,这样修改是O(1)的.查询是O(sqrt(n))的.
然后就可以通过了.
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 100010using namespace std;int a[N],c[N],bl[N],n,m,f[N],bk,cnt,ans[N*10];struct use{int l,r,a,b,id;}q[N*10];struct block{int l,r;}st[N];int read(){int x=0;char ch=getchar();while(ch>'9'||ch<'0') ch=getchar();while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x;}void getbk(){ bk=sqrt(n/2); if (n%bk) cnt=n/bk+1; else cnt=n/bk; for (int i=1;i<=n;i++) bl[i]=(i-1)/bk+1; for (int i=1;i<=cnt;i++){ st[i].l=(i-1)*bk+1; st[i].r=i*bk; }}bool cmp(use a,use b){ if (bl[a.l]==bl[b.l]) return a.r<b.r; else return a.l<b.l;}void add(int x){ c[x]++;if (c[x]==1) f[bl[x]]++;}void del(int x){ c[x]--;if (c[x]==0) f[bl[x]]--;}int query(int a,int b){ int x=bl[a],y=bl[b],ans(0); for (int i=x+1;i<=y-1;i++) ans+=f[i]; if (x==y){ for (int i=a;i<=b;i++) if (c[i]>0) ans++; } else{ for (int i=a;i<=st[bl[a]].r;i++) if (c[i]>0) ans++; for (int i=st[bl[b]].l;i<=b;i++) if (c[i]>0) ans++; } return ans;}void solve(){ int l(0),r(0); for (int i=1;i<=m;i++){ while (l<q[i].l){del(a[l]);l++;}while (l>q[i].l){l--;add(a[l]);}while (r<q[i].r){r++;add(a[r]);}while (r>q[i].r){del(a[r]);r--;}ans[q[i].id]=query(q[i].a,q[i].b); }}int main(){ n=read();m=read(); for (int i=1;i<=n;i++) a[i]=read(); getbk(); for (int i=1;i<=m;i++) q[i].l=read(),q[i].r=read(),q[i].a=read(),q[i].b=read(),q[i].id=i; sort(q+1,q+m+1,cmp); solve(); for (int i=1;i<=m;i++) printf("%d\n",ans[i]);}
0 0
- [BZOJ3809]Gty的二逼妹子序列(莫队+分块)
- 【bzoj3809】【GTY的二逼妹子序列】【莫队+分块】
- 【bzoj3809】Gty的二逼妹子序列 莫队+分块
- 【BZOJ3809】Gty的二逼妹子序列 (莫队+分块)
- bzoj3809 Gty的二逼妹子序列(莫队+分块)
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
- bzoj3809: Gty的二逼妹子序列
- BZOJ3809: Gty的二逼妹子序列
- [bzoj3809]Gty的二逼妹子序列
- 【bzoj3809】Gty的二逼妹子序列
- bzoj3809 Gty的二逼妹子序列
- BZOJ3809: Gty的二逼妹子序列
- BZOJ3809 Gty的二逼妹子序列
- bzoj3809: Gty的二逼妹子序列
- BZOJ3809[Gty的二逼妹子序列]
- 【BZOJ3809】Gty的二逼妹子序列【莫队算法】【分块】
- [bzoj 3809] Gty的二逼妹子序列 莫队+分块
- 【BZOJ 3809】Gty的二逼妹子序列 莫队+分块
- openlayers3中polygon上添加icon
- Java多线程编程
- CentOS下安装Git和初始化一个Git仓库。
- && || 判断规则
- JAVA IO总结
- 【bzoj3809】【GTY的二逼妹子序列】【莫队+分块】
- 用公众号赚钱,我们还有机会吗
- hihoCoder搜索二---骑士问题---暴力法
- NKOI 3689 奶牛大聚会
- Tomcat的三种模式及并发优化
- Activity(界面)
- UFUN学习板测试与学习
- 【android】:Linkify的使用
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIV