【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
3809: Gty的二逼妹子序列
Time Limit: 80 Sec Memory Limit: 28 MB
Submit: 1072 Solved: 292
[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
Solution
分块+莫队
很好想,一开始看错题,没写莫队,直接分块+lower_bound然后发现过不了样例...
其实挺好想,对权值分块,带上莫队搞搞就好...
启发:
序列操作统计颜色,可以优先往分块+莫队上搞
莫队的时候,询问的排序很关键..(手误打反了第1,2关键字,居然能过3组..)
Code
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 100010
#define maxm 1000100
int n,m,a[maxn],pos[maxn],num[maxn],an[maxn],bll,bln;
struct Asknode
{
int l,r,a,b,id;
bool operator < (const Asknode & A) const
{
if (pos[l]==pos[A.l]) return r<A.r;
return l<A.l;
}
}q[maxm];
int ans[maxm],qn;
int Query(int l,int r)
{
int ans=;
if (pos[l]==pos[r])
for (int i=l; i<=r; i++) if (num[i]) ans++; else continue;
else
{
for (int i=l; i<=pos[l]*bll; i++) if (num[i]) ans++;
for (int i=(pos[r]-)*bll+; i<=r; i++) if (num[i]) ans++;
}
for (int i=pos[l]+; i<=pos[r]-; i++) ans+=an[i];
return ans;
}
void move1(int x)
{
num[a[x]]--; if (num[a[x]]==) an[pos[a[x]]]--;
}
void move2(int x)
{
num[a[x]]++; if (num[a[x]]==) an[pos[a[x]]]++;
}
int nl=,nr=;
void work(int x)
{
int L=q[x].l,R=q[x].r,id=q[x].id;
while (nl<L) move1(nl),nl++;
while (nr>R) move1(nr),nr--;
while (nl>L) nl--,move2(nl);
while (nr<R) nr++,move2(nr);
ans[id]=Query(q[x].a,q[x].b);
// printf("%d %d %d %d %d\n",x,L,R,id,ans[id]);
}
int main()
{
n=read(),m=read(); bll=sqrt(n/); if (n%bll) bln=n/bll+; else bln=n/bll;
// printf("%d %d\n",bll,bln);
for (int i=; i<=n; i++) a[i]=read(),pos[i]=(i-)/bll+;
// for (int i=1; i<=n; i++) printf("%d\n",pos[i]);
for (int i=; 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+,q+m+);
for (int i=; i<=m; i++) work(i);
for (int i=; i<=m; i++) printf("%d\n",ans[i]);
return ;
}
%%%Gty大哥,%%%块爷,%%%Basker学长
前排围观自己的傻逼错误:

【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法的更多相关文章
- bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)
[题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...
- BZOJ.3809.Gty的二逼妹子序列(分块 莫队)
题目链接 /* 25832 kb 26964 ms 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过 莫队+分块:这样查询虽然变成了sqr ...
- BZOJ 3809 Gty的二逼妹子序列(莫队+分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...
- 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1728 Solved: 513 Description Autumn ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- [bzoj3809]Gty的二逼妹子序列_莫队_分块
Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...
随机推荐
- 单机多实例Tomcat部署
单机单用户基础上, 如何运行多个tomcat实例. 首先是tomcat的目录结构 bin – 包含所有运行tomcat的二进制和脚本文件 lib – 包含tomcat使用的所有共享库 c ...
- Mysql导出函数、存储过程
下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...
- web.config connectionStrings 数据库连接字符串的解释(转载)
先来看一下默认的连接SQL Server数据库配置 1.默认生成 <connectionStrings> <add name="Exa*DB" connectio ...
- swift UIImage加载远程图片和圆角矩形
UIImage这个对象是swift中的图像类,可以使用UIImageView加载显示到View上. 以下是UIImage的构造函数: init(named name: String!) -> U ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 组织机构的名称编号是否允许重复?
通常情况下,一个公司内部的部门名称,编号是不可能重复的.但是是在多公司的情况下,很可能有部门名称重复的问题存在,这时需要允许部门名称重复. 例如一个大型IT公司,在2个地区都有研发部或者客户服务部,这 ...
- noi题库(noi.openjudge.cn) 1.7编程基础之字符串T21——T30
T21:单词替换 描述 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写.现需要将其中的某个单词替换成另一个单词,并输出替 ...
- node基础02:第一个node程序
1.第一个web服务器 var http = require("http"); http.createServer(function(request, response){ res ...
- datahub
https://help.aliyun.com/document_detail/27854.html
- lecture10-模型的结合与全贝叶斯学习
这是Hinton的第10课 这节课有两篇论文可以作为背景或者课外读物<Adaptive mixtures of local experts>和<Improving neural ne ...
- C#链接阿里云KVStore
KVStore的简单介绍 阿里云KVStore兼容Redis.因为KVStore就相当于Redis的服务器端,我们代码只是当作客户端,链接上服务器端就行了,阿里云的KVStore详情文档见,https ...