Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列
Time Limit: 35 Sec Memory Limit: 28 MB
Submit: 868 Solved: 234
[Submit][Status][Discuss]
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
Source
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define MAXM 1000010
struct node
{
int l,r,a,b,id;
}q[MAXM];
int tot[MAXN],sum[],pos[MAXN],C[MAXN],block,ans[MAXM];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node aa,node bb)
{
if(pos[aa.l]==pos[bb.l])return aa.r<bb.r;
return aa.l<bb.l;
}
void Del(int color)//删除一种颜色
{
tot[color]--;//该种颜色个数-1.
if(tot[color]==)sum[(color-)/block+]--;//以前有的颜色,现在没有了.(个数从1变为0.)
}
void Add(int color)//加上一种颜色
{
tot[color]++;//该种颜色个数+1.
if(tot[color]==)sum[(color-)/block+]++;//以前没有的颜色,现在有了.(个数从0变为1.)
}
int getans(int aa,int bb)
{
int gs=,left=(aa-)/block+,right=(bb-)/block+,i;
if(left==right)
{
for(i=aa;i<=bb;i++)if(tot[i]!=)gs++;
return gs;
}
for(i=aa;i<=left*block;i++)if(tot[i]!=)gs++;
for(i=left+;i<=right-;i++)gs+=sum[i];
for(i=(right-)*block+;i<=bb;i++)if(tot[i]!=)gs++;
return gs;
}
int main()
{
int n,m,i,L,R;
n=read();m=read();
for(i=;i<=n;i++)C[i]=read();
for(i=;i<=m;i++)
{
q[i].l=read();q[i].r=read();q[i].a=read();q[i].b=read();
q[i].id=i;
}
block=(int)sqrt(n);
for(i=;i<=n;i++)pos[i]=(i-)/block+;
sort(q+,q+m+,cmp);
memset(sum,,sizeof(sum));//每一块内的颜色个数.
memset(tot,,sizeof(tot));//枚举的区间内的每种颜色的个数.
L=;R=;
for(i=;i<=m;i++)
{
while(L<q[i].l)
{
Del(C[L]);
L++;
}
while(L>q[i].l)
{
L--;
Add(C[L]);
}
while(R<q[i].r)
{
R++;
Add(C[R]);
}
while(R>q[i].r)
{
Del(C[R]);
R--;
}
ans[q[i].id]=getans(q[i].a,q[i].b);
}
for(i=;i<=m;i++)printf("%d\n",ans[i]);
fclose(stdin);
fclose(stdout);
return ;
}
Bzoj 3809: Gty的二逼妹子序列 莫队,分块的更多相关文章
- 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]\ ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
随机推荐
- js EasyUI前台 价格=数量*单价联动的实现
废话,不多说,,效果图如下:
- oracle 消除块竞争(hot blocks)
上篇日志提到了,那么高的负载,是存在数据块读竞争,下面介绍几个方法来消除块竟争 查找块竟争 SELECT p1 "file#", p2 "block#", p3 ...
- Oracle 的merge into 用法
1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...
- MFC中cannot find the definition (implementation) of this function 解决方法
问题:使用vc6 在点击左侧class view中的一个方法实现时出现下面错误: cannot find the definition (implementation) of this func ...
- 异步调用backgroudworker
先看一个小例子:C#客户端打开一个控件,控件中加载了好多数据大约要用5秒中,如果我们直接打开控件,那么这个控件就要5秒中才能弹出来,当然这个时候用户已经把他Kill了.这个时候我们就需要先给用户把控件 ...
- Window 下安装Redis
下载地址:https://github.com/dmajkic/redis/downloads. 下载到的Redis支持32bit和64bit.根据自己实际情况选择,将64bit的内容cp到自定义盘符 ...
- ecshop 报错
ECShop出现Strict Standards: Only variables should be passed b (2014-06-04 17:00:37) 转载▼ 标签: ecshop 报错 ...
- .Net C/S系统开发框架(楚楚原创)
C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.htm 视频下载: 百度 ...
- windows通过Composer安装yii2
1. php.ini 中;extension=php_openssl.dll(取消注释,不然在安装composer过程中会报错) 集成环境最好去php目录中打开php.ini文件,确定;extensi ...
- 关于if/else if
今天写存储过程的时候发现了个问题,就是在用if.elsif拼接sql的时候,得到的结果跟想象中 的不一样,大概描述是: 我传进去多个参数,然后判断每个字段是否为空,非空则将字段拼接上去,结果跟预料的 ...