【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=3585
好神的题。
但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!!这什么梗。。。。。。。。。。。。。。。。。。。。。。
我思考了很久空间的问题,因为我在pushdown的时候可能会越界,或许是pushup?
不管了。然后看了zyf的写法。看来以后得注意下。。。pushdown弄成先放了。。。
本题的做法:
好神orz
首先像莫队一样离线区间,左端点在前。
考虑如何从[l, r]转移到[l+1, r]:
显然,a[l]此时是去掉了,l+1~next[l]是空着了a[l]这个自然数的。next[l]是下一个a[l]的位置。
所以我们直接线段树更新l+1~next[i]的sg值即可!即如果这个区间内有询问的sg值>a[l],那么更新!
好神。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } #define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, mid, lc
#define rson mid+1, r, rc const int N=200005, oo=~0u>>1;
struct dat { int l, r, id; }q[N];
int sg[N], mn[N<<2];
void upd(int x, int k) { mn[x]=min(mn[x], k); }
void pushdown(int x) {
if(mn[x]!=oo) {
upd(lc, mn[x]);
upd(rc, mn[x]);
mn[x]=oo;
}
}
void build(int l, int r, int x) {
if(l==r) { mn[x]=sg[l]; return; }
mn[x]=oo;
int mid=MID;
build(lson); build(rson);
}
void update(int l, int r, int x, int L, int R, int k) {
if(L<=l && r<=R) { mn[x]=min(mn[x], k); return; }
pushdown(x);
int mid=MID;
if(L<=mid) update(lson, L, R, k);
if(mid<R) update(rson, L, R, k);
}
int query(int l, int r, int x, int k) {
if(l==r) return mn[x];
pushdown(x);
int mid=MID;
if(k<=mid) return query(lson, k);
return query(rson, k);
}
int a[N], b[N], n, m, tot, vis[N], ans[N], inext[N]; void getnext() {
for1(i, 1, n) b[i]=a[i];
sort(b+1, b+1+n); tot=unique(b+1, b+1+n)-b-1;
for1(i, 1, n) a[i]=lower_bound(b+1, b+1+tot, a[i])-b;
for1(i, 1, tot) vis[i]=0;
for3(i, n, 1) inext[i]=vis[a[i]], vis[a[i]]=i;
}
bool cmp(const dat &a, const dat &b) { return a.l<b.l; }
void init() {
int k=0;
for1(i, 1, n) {
if(a[i]<N) vis[a[i]]=1;
while(vis[k]) ++k;
sg[i]=k;
}
build(1, n, 1);
sort(q+1, q+1+m, cmp);
getnext();
int now=1;
for1(i, 1, m) {
int l=q[i].l;
while(now<l) {
int nxt=inext[now];
if(nxt==0) nxt=n;
else --nxt; // printf("now:%d nxt:%d a[now]:%d\n", now, nxt, b[a[now]]);
if(now+1<=nxt) update(1, n, 1, now+1, nxt, b[a[now]]);
++now;
}
ans[q[i].id]=query(1, n, 1, q[i].r);
}
for1(i, 1, m) printf("%d\n", ans[i]);
} int main() {
read(n); read(m);
for1(i, 1, n) read(a[i]);
for1(i, 1, m) read(q[i].l), read(q[i].r), q[i].id=i;
init();
return 0;
}
Description
有一个长度为n的数组{a1,a2,...,an}。m次询问,每次询问一个区间内最小没有出现过的自然数。
Input
第一行n,m。
第二行为n个数。
从第三行开始,每行一个询问l,r。
Output
一行一个数,表示每个询问的答案。
Sample Input
2 1 0 2 1
3 3
2 3
2 4
1 2
3 5
Sample Output
2
3
0
3
HINT
数据规模和约定
对于100%的数据:
1<=n,m<=200000
0<=ai<=109
1<=l<=r<=n
对于30%的数据:
1<=n,m<=1000
Source
【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)的更多相关文章
- Bzoj 3339: Rmq Problem && Bzoj 3585: mex 莫队,树状数组,二分
3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 833 Solved: 397[Submit][Status][D ...
- BZOJ 3339: Rmq Problem
3339: Rmq Problem Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1075 Solved: 549[Submit][Status][ ...
- [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树
Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...
- BZOJ 3339: Rmq Problem 莫队算法
3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...
- BZOJ 3339 Rmq Problem(离线+线段树+mex函数)
题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...
- bzoj 3585 mex - 线段树 - 分块 - 莫队算法
Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行开始,每行一个询问 ...
- BZOJ.3585.mex(线段树)
题目链接 题意:多次求区间\(mex\). 考虑\([1,i]\)的\(mex[i]\),显然是单调的 而对于\([l,r]\)与\([l+1,r]\),如果\(nxt[a[l]]>r\),那么 ...
- CF803G-Periodic RMQ Problem【离散化,线段树,ST表】
正题 题目链接:https://www.luogu.com.cn/problem/CF803G 题目大意 一个长度为\(n\)的序列\(a\)复制\(k\)份连接,要求支持 区间赋值 区间查询最小值 ...
- BZOJ 3489 A simple rmq problem(可持久化线段树)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...
随机推荐
- bash read命令用法
read -p "Enter your student ID: " USERNAMEread -s -p "Enter your password: " PAS ...
- css整个页面离顶部的距离
body { padding:0; margin:0; font-size:12px; line-height:22px; } 说明: 整个页面离顶部的距离是22像素
- C++基础知识面试精选100题系列(1-10题)[C++ basics]
[原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...
- iOS UIDatePicker frame改变问题
这种方法不行: pickerCtl = UIDatePicker(frame:pickerFrame) 但是这种却行 pickerCtl = UIDatePicker() pickerCtl!.fra ...
- c# 获取屏幕DPI
方法一:用ManagementClass来获取.需要引入System.Management.dll; using (ManagementClass mc = new ManagementClass(& ...
- Java for LeetCode 148 Sort List
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log ...
- C++动态内存管理之shared_ptr、unique_ptr
C++中的动态内存管理是通过new和delete两个操作符来完成的.new操作符,为对象分配内存并调用对象所属类的构造函数,返回一个指向该对象的指针.delete调用时,销毁对象,并释放对象所在的内存 ...
- linux架构图
/ 根目录 │ ├boot/ 启动文件.所有与系统启动有关的文件都保存在这里 │ └grub/ Grub引导器相关的文件 │ ├dev/ 设备文件 ├proc/ 内核与进程镜像 │ ├mnt/ 临时挂 ...
- Android Studio安装与配置
谷歌已经停止支持eclipse开发android了,转向android studio是大势所趋,笔者由于电脑配置的原因, 以前迟迟不愿意向android studio,现如今因为开始学习materia ...
- .NET生成word文档服务器配置常见问题
注意:安装office2003的时候一定要选择 "完全安装" 而不是 "典型安装" 错误:System.Runtime.InteropServices.COME ...