【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]\ ...
随机推荐
- 资料,来自HTML5前端开发学习⑤群
resource HTML5+CSS3视频教程:http://pan.baidu.com/s/1hsyOjze 密码:c3uw JavaScript视频教程:链接:http://pan.baidu.c ...
- Ubuntu终端命令行不显示颜色
在网上找到的一个有效方案是在.bash_profile 中增加颜色定义 export LS_COLORS='di=01;35:ln=01;36:pi=40;33:so=01;35:do=01;35:b ...
- Scrapy 爬虫
Scrapy 爬虫 使用指南 完全教程 scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name ...
- 基于LeNet网络的中文验证码识别
基于LeNet网络的中文验证码识别 由于公司需要进行了中文验证码的图片识别开发,最近一段时间刚忙完上线,好不容易闲下来就继上篇<基于Windows10 x64+visual Studio2013 ...
- Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- scala 学习笔记(02) 元组Tuple、数组Array、Map、文件读写、网页抓取示例
package yjmyzz import java.io.PrintWriter import java.util.Date import scala.io.Source object ScalaA ...
- Ubuntu环境下安装TinyOS系统
1.输入下面命令会弹出source list窗口 1 sudo gedit /etc.apt/sources.list 在尾部添加以下地址: 1 2 deb http://tinyos.sta ...
- SQL 性能调优日常积累
我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1)选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE 的解析器按照从右到左 ...
- checkbox js onclick ajax,列表页表格中修改数据
<input type='checkBox' value='".$row["p_id"]."' onclick='changeisNew(this);'& ...
- SQL Server 2012新特性(1)T-SQL操作FileTable目录实例
在SQL Server 2008提供FileStream,以借助Windows系统本身的API来强化SQL Server对于非结构化数据的支持后,SQL Server 2012更是推出了像Contai ...