【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
【BZOJ3809】Gty的二逼妹子序列
Description
Input
Output
对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。
Sample Input
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
0
0
2
1
1
1
0
1
2
HINT
题解:一看到题第一感觉仍然是莫队+树状数组,但是时间复杂度O(m*sqrt(n)*log(n)),承受不起啊,但是我们可以分块
对于原来的算法,修改时O(m*sqrt(n)*log(n))的,但是查询却是O(m*log(n))的,我们用分块相当于牺牲一点查询的时间,使修改更快一点
言归正传,我们只需要将权值分块,维护每个块内不同权值的种类数以及区间中每个权值的出现次数,然后查询时先查询[a,b]中间的块的种类数,在暴力统计两边的块内的出现次数,于是修改和查询都是O(m*sqrt(n))的了
别忘了特判a,b在一个块内的情况
从1开始的分块真的很别扭啊~
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n,m,siz;
struct node
{
int qa,qb,ql,qr,org;
}q[1000010];
int v[100010],sk[100010],s[100010],ans[1000010];
int rd()
{
int ret=0; char gc=getchar();
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
bool cmp(node a,node b)
{
if((a.ql-1)/siz==(b.ql-1)/siz) return a.qr<b.qr;
return (a.ql-1)/siz<(b.ql-1)/siz;
}
int main()
{
n=rd(),m=rd();
siz=(int)sqrt((double)n);
int i,j;
for(i=1;i<=n;i++) v[i]=rd();
for(i=1;i<=m;i++) q[i].ql=rd(),q[i].qr=rd(),q[i].qa=rd(),q[i].qb=rd(),q[i].org=i;
sort(q+1,q+m+1,cmp);
int l=1,r=0;
for(i=1;i<=m;i++)
{
while(r<q[i].qr) r++,sk[(v[r]-1)/siz]+=(s[v[r]]==0),s[v[r]]++;
while(r>q[i].qr) s[v[r]]--,sk[(v[r]-1)/siz]-=(s[v[r]]==0),r--;
while(l>q[i].ql) l--,sk[(v[l]-1)/siz]+=(s[v[l]]==0),s[v[l]]++;
while(l<q[i].ql) s[v[l]]--,sk[(v[l]-1)/siz]-=(s[v[l]]==0),l++;
if((q[i].qa-1)/siz==(q[i].qb-1)/siz)
{
for(j=q[i].qa;j<=q[i].qb;j++) ans[q[i].org]+=(s[j]>0);
continue;
}
for(j=q[i].qa;j<=(q[i].qa-1)/siz*siz+siz&&j<=n;j++) ans[q[i].org]+=(s[j]>0);
for(j=(q[i].qb-1)/siz*siz+1;j<=q[i].qb;j++) ans[q[i].org]+=(s[j]>0);
for(j=(q[i].qa-1)/siz+1;j<(q[i].qb-1)/siz;j++) ans[q[i].org]+=sk[j];
}
for(i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}
【BZOJ3236】[Ahoi2013]作业
别的和上题都一样,就是新增一个求[l,r]中数值∈[a,b]的数的个数,这个怎么搞都可以吧~
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n,m,siz;
struct node
{
int qa,qb,ql,qr,org;
}q[1000010];
int v[100010],sk[100010],sv[100010],s[100010],ans[1000010],sum[1000010];
int rd()
{
int ret=0; char gc=getchar();
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
bool cmp(node a,node b)
{
if((a.ql-1)/siz==(b.ql-1)/siz) return a.qr<b.qr;
return (a.ql-1)/siz<(b.ql-1)/siz;
}
int main()
{
n=rd(),m=rd();
siz=(int)sqrt((double)n);
int i,j;
for(i=1;i<=n;i++) v[i]=rd();
for(i=1;i<=m;i++) q[i].ql=rd(),q[i].qr=rd(),q[i].qa=rd(),q[i].qb=rd(),q[i].org=i;
sort(q+1,q+m+1,cmp);
int l=1,r=0;
for(i=1;i<=m;i++)
{
while(r<q[i].qr) r++,sk[(v[r]-1)/siz]+=(s[v[r]]==0),s[v[r]]++,sv[(v[r]-1)/siz]++;
while(r>q[i].qr) s[v[r]]--,sv[(v[r]-1)/siz]--,sk[(v[r]-1)/siz]-=(s[v[r]]==0),r--;
while(l>q[i].ql) l--,sk[(v[l]-1)/siz]+=(s[v[l]]==0),s[v[l]]++,sv[(v[l]-1)/siz]++;
while(l<q[i].ql) s[v[l]]--,sv[(v[l]-1)/siz]--,sk[(v[l]-1)/siz]-=(s[v[l]]==0),l++;
if((q[i].qa-1)/siz==(q[i].qb-1)/siz)
{
for(j=q[i].qa;j<=q[i].qb;j++) ans[q[i].org]+=(s[j]>0),sum[q[i].org]+=s[j];
continue;
}
for(j=q[i].qa;j<=(q[i].qa-1)/siz*siz+siz&&j<=n;j++) ans[q[i].org]+=(s[j]>0),sum[q[i].org]+=s[j];
for(j=(q[i].qb-1)/siz*siz+1;j<=q[i].qb;j++) ans[q[i].org]+=(s[j]>0),sum[q[i].org]+=s[j];
for(j=(q[i].qa-1)/siz+1;j<(q[i].qb-1)/siz;j++) ans[q[i].org]+=sk[j],sum[q[i].org]+=sv[j];
}
for(i=1;i<=m;i++) printf("%d %d\n",sum[i],ans[i]);
return 0;
}
【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块的更多相关文章
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1072 Solved: 292[Submit][Status][Di ...
- 【bzoj3809】Gty的二逼妹子序列
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...
- 莫队p2 【bzoj3809】Gty的二逼妹子序列
发现一篇已经够长了...所以就放在这里吧... http://hzwer.com/5749.html ↑依然是看大牛题解过的 袜子那道题太简单了.... 然后被这道题超时卡了一段时间....... ...
- BZOJ3809:Gty的二逼妹子序列
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- [bzoj3809]Gty的二逼妹子序列_莫队_分块
Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...
- [bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业
[bzoj3809]Gty的二逼妹子序列/[bzoj3236][Ahoi2013]作业 bzoj bzoj 题目大意:一个序列,m个询问在$[l,r]$区间的$[x,y]$范围内的数的个数/种类. ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
随机推荐
- 基于Bootstrap的Asp.net Mvc 分页的实现(转)
最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一个Mvc 项目,既然是分页就需要一些数据,我这 边是模拟了一 ...
- [转]sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...
- Mysql使用大全 从基础到存储过程
平常习惯了phpmyadmin等其他工具的的朋友有的根本就不会命令,如果让你笔试去面试我看你怎么办,所以,学习一下还是非常有用的,也可以知道你通过GUI工具的时候工具到底做了什么.Mysql用处很广, ...
- Animation.Sample用法介绍
无意中翻到这篇问答LINK,发现了Sample的用法 如果想让Animation在编辑器状态下预览,也可以用这个接口 当你想要直接获得动画的运行结果,而不是等帧数执行到这,这时候就得调用Sample: ...
- 分布式ID生成方案
系统唯一ID是设计一个系统的时候常常会遇到的问题,也常常为这个问题而纠结. 生成ID的方法有很多,适应不同的场景.需求以及性能要求.所以有些比较复杂的系统会有多个ID生成的策略. 0. 分布式ID要求 ...
- [svc]后台运行程序screen or nohup
后台运行 方法1 & 方法2:screen screen –S lnmp à起个名字 进去后运行程序 Ctrl+ad à退出lnmp屏幕 Scree –ls à查看 Screen –r xxx ...
- 【转】如何把hadoop-1.x源码关联到Eclipse工程
[转]http://www.tuicool.com/articles/mIb2EzU
- c#第一个程序-计算平方根
上课教的内容.做笔记了. using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- systemd启动多实例
最近用了centos7,启动管理器用的是systemd,感觉很好玩. 1.开机自动启动 新建一个service文件放到/usr/lib/systemd/system/ 比如: [Unit] Descr ...
- 源码分享!!!world文档转换为JPG图片
http://bbs.csdn.net/topics/390055515 —————————————————————————————————————————————————— 基本思路是:先将worl ...