【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805283.html
题目描述
有一个长度为n的数组{a1,a2,...,an}。m次询问,每次询问一个区间内最小没有出现过的自然数。
输入
第一行n,m。
第二行为n个数。
从第三行开始,每行一个询问l,r。
输出
一行一个数,表示每个询问的答案。
样例输入
5 5
2 1 0 2 1
3 3
2 3
2 4
1 2
3 5
样例输出
1
2
3
0
3
题解
莫队算法+分块,双倍经验题
首先必有如果某数大于等于n,那么它对答案没有任何贡献,所以可以把大于n的数看成n
然后类似于bzoj3809,将权值分块,查询时先找到第一个不满的块,再在块中查找。
注意:自然数:自然数集是全体非负整数组成的集合(包括0),所以分块要从0开始。
#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 200010
using namespace std;
struct data
{
int l , r , bl , id;
}a[N];
int w[N] , cnt[N] , num[510] , ans[N];
bool cmp(data a , data b)
{
return a.bl == b.bl ? a.r < b.r : a.bl < b.bl;
}
int main()
{
int n , m , si , i , j , lp = 1 , rp = 0;
scanf("%d%d" , &n , &m) , si = (int)sqrt(n);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d" , &w[i]);
if(w[i] > n) w[i] = n;
}
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &a[i].l , &a[i].r) , a[i].bl = (a[i].l - 1) / si , a[i].id = i;
sort(a + 1 , a + m + 1 , cmp);
n ++ , si = (int)sqrt(n);
for(i = 1 ; i <= m ; i ++ )
{
while(lp > a[i].l) lp -- , num[w[lp] / si] += (!cnt[w[lp]]) , cnt[w[lp]] ++ ;
while(rp < a[i].r) rp ++ , num[w[rp] / si] += (!cnt[w[rp]]) , cnt[w[rp]] ++ ;
while(lp < a[i].l) cnt[w[lp]] -- , num[w[lp] / si] -= (!cnt[w[lp]]) , lp ++ ;
while(rp > a[i].r) cnt[w[rp]] -- , num[w[rp] / si] -= (!cnt[w[rp]]) , rp -- ;
for(j = 0 ; j <= (n - 1) / si ; j ++ ) if(num[j] < si) break;
j *= si;
while(cnt[j]) j ++ ;
ans[a[i].id] = j;
}
for(i = 1 ; i <= m ; i ++ ) printf("%d\n" , ans[i]);
return 0;
}
【bzoj3585/bzoj3339】mex/Rmq Problem 莫队算法+分块的更多相关文章
- BZOJ 3339: Rmq Problem 莫队算法
3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...
- 【题解】P4137 Rmq Problem(莫队)
[题解]P4137 Rmq Problem(莫队) 其实这道题根本就不用离散化! 因为显然有\(mex\)值是\(\le 2\times 10^5\)的,所以对于大于\(2\times 10^5\)的 ...
- 【bzoj3809/bzoj3236】Gty的二逼妹子序列/[Ahoi2013]作业 莫队算法+分块
原文地址:http://www.cnblogs.com/GXZlegend/p/6805252.html bzoj3809 题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了 ...
- 【BZOJ3585/3339】mex 莫队算法+分块
[BZOJ3585]mex Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- XOR and Favorite Number(莫队算法+分块)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- WHU-1551-Pairs(莫队算法+分块实现)
Description Give you a sequence consisted of n numbers. You are required to answer how many pairs of ...
随机推荐
- git 简单使用(待完善)
git是一个分布式版本控制器,简单来说就是可以记录每次代码的修改和提交,方便我们查看修改记录和版本的回退 工作流程 基本概念 仓库 git 是一个分布式版本控制器,其单位就是仓库,每个仓库就是当前gi ...
- js延迟执行与循环执行
延迟一段时间执行特定代码: setTimeout(function () { window.location.href = 'login' },1200); 循环执行: function test() ...
- Java分享笔记:Map集合(接口)的基本方法程序演示
package pack02; import java.util.*; public class MapDemo { public static void main(String[] args) { ...
- opencms研究笔记
最近公司一新产品,众多选型之后: 最后还是准备在用opencms 在opencms的基础上,进行二次开发: 有一起研究的没 欢迎交流:
- 详解JavaScript中的arc的方法
今天说说JavaScript在网页中画圆的函数arc! 一.arc所需要的参数设置 1 arc(x, y, radius, startAngle, endAngle, counterclockwise ...
- MySQL查询优化 对not in 、in 的优化
因为 not in不走索引,所以不在不得已情况下,就不要使用not in 下面使用 join 来替代not in 做查询 select ID from A where ID not in (selec ...
- 使用python写一个最基本的mapreduce程序
一个mapreduce程序大致分成三个部分,第一部分是mapper文件,第二个就是reducer文件,第三部分就是使用hadoop command 执行程序. 在这个过程中,困惑我最久的一个问题就是在 ...
- iOS常用控件-UITableViewCell
一. 封装cell: 1.加载xib文件的两种方式 <方式1> (NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] ...
- Drazil and Tiles CodeForces - 516B (类拓扑)
Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a gr ...
- 010---Django的模型层(2)
确定模型关系: ''' Publish ---- Book 多对一:一个出版社对应多本书,在多的那张表添加关联字段 Book ---- Author 多对多:一个书对应多个作者,多个作者对应一本书 会 ...