Luogu 4137 Rmq Problem / mex
一个主席树题。
一开始想着直接动态开点硬搞就可以了,每次查询只要作一个类似于前缀和的东西看看区间有没有满,在主席树上二分就可以了。
但是这样是错的,因为一个权值会出现很多次……然后就错了。
所以我们考虑记录每一个权值最后出现的位置,直接开权值下标记录每一个权值最后出现的位置,因为是区间查询,所以可持久化一下,这样答案就是第一次出现位置小于$l$的最小权值,查询方法类似。
考虑到答案只可能是$a_{i} + 1, 0$,所以直接大力把$a_{i}, a_{i} + 1,0$都丢进去离散化。
注意线段树中权值0出现的位置不是inf,因为0也算自然数。
感觉离线下来也可以不用写可持久化。
自己一开始还是naive
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 4e5 + ;
const int M = 5e6 + ;
const int inf = << ; int n, qn, tot = , a[N], b[N]; inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} namespace PSegT {
struct Node {
int lc, rc, data;
} s[M]; int root[N], nodeCnt; #define mid ((l + r) >> 1) inline void up(int p) {
if(p) s[p].data = min(s[s[p].lc].data, s[s[p].rc].data);
} void ins(int &p, int l, int r, int x, int pre, int v) {
p = ++nodeCnt;
s[p].lc = s[pre].lc, s[p].rc = s[pre].rc;
if(l == r) {
s[p].data = v;
return;
} if(x <= mid) ins(s[p].lc, l, mid, x, s[pre].lc, v);
else ins(s[p].rc, mid + , r, x, s[pre].rc, v);
up(p);
} int query(int p, int l, int r, int x) {
if(!p || l == r) return b[l]; if(s[s[p].lc].data < x) return query(s[p].lc, l, mid, x);
else return query(s[p].rc, mid + , r, x);
} } using namespace PSegT; int main() {
read(n), read(qn);
b[++tot] = ;
for(int i = ; i <= n; i++) {
read(a[i]);
b[++tot] = a[i], b[++tot] = a[i] + ;
} sort(b + , b + tot + );
tot = unique(b + , b + + tot) - b - ;
root[] = nodeCnt = ; //s[0].data = inf;
for(int i = ; i <= n; i++) {
a[i] = lower_bound(b + , b + + tot, a[i]) - b;
ins(root[i], , tot, a[i], root[i - ], i);
} for(int x, y; qn--; ) {
read(x), read(y);
printf("%d\n", query(root[y], , tot, x));
}
return ;
}
Luogu 4137 Rmq Problem / mex的更多相关文章
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- Luogu P4137 Rmq Problem / mex
区间mex问题,可以使用经典的记录上一次位置之后再上主席树解决. 不过主席树好像不是很好写哈,那我们写莫队吧 考虑每一次维护什么东西,首先记一个答案,同时开一个数组记录一下每一个数出现的次数. 然后些 ...
- 【luogu P4137 Rmq Problem / mex】 题解
题目链接:https://www.luogu.org/problemnew/show/P4137 求区间内最大没出现过的自然数 在add时要先判断会不会对当前答案产生影响,如果有就去找下一个答案. # ...
- luogu P4137 Rmq Problem / mex 主席树 + 思维
Code: #include<bits/stdc++.h> #define maxn 200001 using namespace std; void setIO(string s) { ...
- 洛谷$P$4137 $Rmq\ Problem / mex$ 主席树
正解:主席树 解题报告: 传送门$QwQ$ 本来以为是道入门无脑板子题,,,然后康了眼数据范围发现并没有我想像的那么简单昂$kk$ 这时候看到$n$的范围不大,显然考虑离散化?但是又感觉似乎布星?因为 ...
- luogu P4137 Rmq Problem / mex(可持久化线段树)
一开始想的是莫队,然后维护几个bitset,然后瞎搞.脑子里想了想实现,发现并不好写. 还是主席树好写.我们维护一个权值的线段树,记录每一个权值的最后一次出现的位置下标.我们查询的时候要在前\(r\) ...
- [bzoj3585] Rmq Problem / mex
[bzoj3585] Rmq Problem / mex bzoj luogu 看上一篇博客吧,看完了这个也顺理成章会了( (没错这篇博客就是这么水) #include<cstdio> # ...
- 【Luogu4137】Rmq Problem/mex (莫队)
[Luogu4137]Rmq Problem/mex (莫队) 题面 洛谷 题解 裸的莫队 暴力跳\(ans\)就能\(AC\) 考虑复杂度有保证的做法 每次计算的时候把数字按照大小也分块 每次就枚举 ...
- P4137 Rmq Problem / mex (莫队)
题目 P4137 Rmq Problem / mex 解析 莫队算法维护mex, 往里添加数的时候,若添加的数等于\(mex\),\(mex\)就不能等于这个值了,就从这个数开始枚举找\(mex\): ...
随机推荐
- UOJ #55 & 洛谷 P3920 紫荆花之恋 —— 动态点分治+替罪羊树
题目:http://uoj.ac/problem/55 https://www.luogu.org/problemnew/show/P3920 参考博客:https://www.cnblogs.com ...
- c++11之三: sizeof运算符 auto的优势 __func__预定义标识符
在C++11中,对非静态成员变量使用sizeof操作是合法的. auto推导的一个最大优势就是在拥有初始化表达式的复杂类型变量声明时简化代码.如:std:vector<std::string&g ...
- 编译PHP扩展的通用方法
以安装swoole扩展为例: 步骤1: wget pecl.php.net/get/swoole-1.7.21.tgz (下载swoole打包文件) 步骤2: tar zxvf swoole-1. ...
- [C++] 分治法之棋盘覆盖、循环赛日程表
一.分治的基本思想 将一个难以直接解决的大问题,分割成一些规模较小的相同问题,以便各个击破,分而治之. 对于一个规模为 n 的问题,若问题可以容易地解决,则直接解决,否则将其分解为 k 个规模较小的子 ...
- POJ3641(快速幂)
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8529 Accepted: 35 ...
- Spring Boot Starter 速查
Spring Boot应用启动器基本的一共有40多种,常用的如下 1)spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2)spring ...
- SQL 从身份证号得到出生日期、年龄、男女
), CONVERT(smalldatetime, SUBSTRING(b.IDCard, , )), ) AS BrithDate_Name, DATEDIFF(year, CONVERT(smal ...
- eclipse项目中将普通文件夹转化成资源文件夹
1.点选该文件夹 2.右键属性Properties 3.选择属性:Build Path 4.点选属性:Use as Source Folder ......等待变成资源文件夹 参考更详细的: ecl ...
- Python操作Excel,并结合unittest单元测试框架
第一步:写Excel操作方法 excel_operate.py文件 from openpyxl import load_workbook #引入模块 class MyExcel: def __init ...
- &(((struct A*)NULL)->m_float)---offsetof
问题描述: struct A { int m_int; float m_float; }; int main(void) { printf("%p",&(((struct ...