题目链接:https://www.luogu.org/problemnew/show/P4137

求区间内最大没出现过的自然数

在add时要先判断会不会对当前答案产生影响,如果有就去找下一个答案。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 2e5+;
int curL = , curR = , n, m, a[maxn], answer = , bl, cnt[maxn], ans[maxn];
struct query{
int l, r, p;
}q[maxn];
inline int read()
{
int ret=;
char c=getchar();
while (c<'' || c>'') c=getchar();
while (c>='' && c<=''){
ret=((ret<<)+(ret<<))+c-'';
c=getchar();
}
return ret;
}
bool cmp(const query &a, const query &b)
{
if(a.l / bl == b.l / bl) return a.r < b.r;
else return a.l < b.l;
}
void add(int pos)
{
cnt[a[pos]]++;
if(cnt[a[pos]] == && answer==a[pos])
{
while(cnt[answer]!=) answer++;
}
}
inline void remove(int pos)
{
cnt[a[pos]]--;
if (cnt[a[pos]]==) answer=min(answer,a[pos]);
}
int main()
{
n = read(); m = read();
bl = sqrt(n); for(int i = ; i <= n; i++)
a[i] = read(); for(int i = ; i <= m; i++)
{
q[i].l = read(); q[i].r = read();
q[i].p = i;
}
sort(q+,q++m,cmp);
for(int i = ; i <= m; i++)
{
int L = q[i].l, R = q[i].r;
while(curL < L) remove(curL++);
while(curL > L) add(--curL);
while(curR < R) add(++curR);
while(curR > R) remove(curR--);
ans[q[i].p] = answer;
}
for(int i = ; i <= m; i++)
printf("%d\n",ans[i]);
return ;
}

【luogu P4137 Rmq Problem / mex】 题解的更多相关文章

  1. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  2. luogu P4137 Rmq Problem / mex 主席树 + 思维

    Code: #include<bits/stdc++.h> #define maxn 200001 using namespace std; void setIO(string s) { ...

  3. Luogu P4137 Rmq Problem / mex

    区间mex问题,可以使用经典的记录上一次位置之后再上主席树解决. 不过主席树好像不是很好写哈,那我们写莫队吧 考虑每一次维护什么东西,首先记一个答案,同时开一个数组记录一下每一个数出现的次数. 然后些 ...

  4. luogu P4137 Rmq Problem / mex(可持久化线段树)

    一开始想的是莫队,然后维护几个bitset,然后瞎搞.脑子里想了想实现,发现并不好写. 还是主席树好写.我们维护一个权值的线段树,记录每一个权值的最后一次出现的位置下标.我们查询的时候要在前\(r\) ...

  5. 洛谷 P4137 Rmq Problem/mex 题解

    题面 首先,由于本人太菜,不会莫队,所以先采用主席树的做法: 离散化是必须环节,否则动态开点线段数都救不了你: 我们对于每个元素i,插入到1~(i-1)的主席树中,第i颗线段树(权值线段树)对于一个区 ...

  6. P4137 Rmq Problem / mex (莫队)

    题目 P4137 Rmq Problem / mex 解析 莫队算法维护mex, 往里添加数的时候,若添加的数等于\(mex\),\(mex\)就不能等于这个值了,就从这个数开始枚举找\(mex\): ...

  7. 洛谷 P4137 Rmq Problem /mex 解题报告

    P4137 Rmq Problem /mex 题意 给一个长为\(n(\le 10^5)\)的数列\(\{a\}\),有\(m(\le 10^5)\)个询问,每次询问区间的\(mex\) 可以莫队然后 ...

  8. 洛谷 P4137 Rmq Problem / mex

    https://www.luogu.org/problemnew/show/P4137 只会log^2的带修主席树.. 看了题解,发现有高妙的一个log做法:权值线段树上,设数i对应的值ma[i]为数 ...

  9. P4137 Rmq Problem / mex

    目录 链接 思路 线段树 莫队 链接 https://www.luogu.org/problemnew/show/P4137 思路 做了好几次,每次都得想一会,再记录一下 可持久化权值线段树 区间出现 ...

随机推荐

  1. TOJ 4393 Game

    描述 Bob always plays game with Alice.Today,they are playing a game on a tree.Alice has m1 stones,Bob ...

  2. 【python爬虫】用python编写LOL战绩查询

    介绍一个简单的python爬虫,通过Tkinter创建一个客户端,当输入要查询的LOL用户名称的时候,可以显示出当前用户的所在服务器,当前战力和当前段位. 爬取网页地址:http://lol.duow ...

  3. elasticSearch请求流程图

  4. BNU 20950 ——沉重的货物 —————— · 最短路、最短边最大化」

    沉重的货物 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: ...

  5. eclipse-java-style.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><profil ...

  6. Java笔记之Scanner先读取一个数字,在读取一行字符串方法分析

    问题:大家在学习Java读取数据的时候一般都是使用Scanner方法读取数据,但是其中有一个小问题大家可能不知道, 就是我们在使用scanner的时候如果你先读取一个数字,在读取一行带有空格的字符串, ...

  7. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. hibernate从零开始到各种映射

    ORM(对象/关系数据库映射) 对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.它完成了面向对象的编程语言到 ...

  9. 从零开始的全栈工程师——underscore

    underscore 是js封装的一个js库 库和框架是有区别的 mvc框架就是backbone就是依赖这个库underscore就是定义了一个_( 下划线对象 ); 函数库的所有的方法都归属于这个对 ...

  10. 闭包中的this

    var name="pushline";//全局变量 var obj=new Object(); obj.name="jms"; obj.getName=fun ...