【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]\ ...
随机推荐
- Centos6 安装 Redis
先确认gcc和tcl已经安装 sudo yum install gcc-c++ sudo yum install tcl 解压, 编译和安装 .tar.gz /usr/src/ cd /usr/src ...
- Codevs 1910递归函数
1910 递归函数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 对于一个递归函数w(a, b, c). 如果a <= ...
- QT QDateTime类、QTimer类
QDateTime类,头文件#include <QDateTime> 可以使用QDateTime类来获得系统时间.通过QDateTime::currentDateTime()来获取本地系统 ...
- Java7并发编程实战(一) 守护线程的创建和运行
Java里有一种特殊的线程叫做守护(Daemon)线程,这种线程的优先级很低,通常来说,当一个应用程序里面没有其他线程运行的时候,守护线程才运行,当线程是程序中唯一运行的线程时,守护线程执行结束后,J ...
- Windbg调优Kafka.Client内存泄露
从来没写过Blog,想想也是,工作十多年了,搞过N多的架构.技术,不与大家分享实在是可惜了.另外,从传统地ERP行业转到互联网,也遇到了很所前所未有的问题,原来知道有一些坑,但是不知道坑太多太深.借着 ...
- bash中变量+=,if大小判断,随机休眠
#!/bin/bash index= while true;do echo "hello" (( index+=)) echo `date "+%H:%M:%S" ...
- Retro 2013
现在的team里每个迭代都会做一次retro,回顾这两周的情况,有哪些做得好的地方,有哪些做得不足的地方,并制定出一系列action,以期望在下一个迭代中解决这些问题.我觉得这种形式挺不错.因此今年的 ...
- json数据转换失败json_last_error int(4)
最近在程序中使用json_decode($data,true);转换失败, 并且使用 json_last_error();方法返回最后一次转换的错误,json_last_error();返回int(4 ...
- iOS 自定义NavigationBar右侧按钮rightBarButtonItem--button
//两个按钮的父类view UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; //历史浏览按钮 ...
- php 实现创建文件并追加数据
最近因为后台有其他事情忙,所以我最近又开始学习php的内容了. (不过话说回来从客户端写到后台的感觉还是很爽的,嘿嘿) 需求是这样:从前台发来一些信息,存成文本文档,以后再统一处理(比如,存入用户账户 ...