题意:

q次询问,问[l,r]子区间的mex值

思路:

对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减

对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r]的mex

l每增大1,都要更新[l, next[l]-1],将这个区间内大于a[l]的值更新为a[l]

其中next[i]为i之后的下一个a[i]位置

代码:

线段树写的不熟。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
//#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f; const db pi = acos(-1.0); int a[maxn];
int n, q;
int nx[maxn];
int lst[maxn];
struct node{
int l, r;
int id;
}b[maxn];
bool cmp(node a, node b){
return a.l<b.l;
}
int mex[maxn];
set<int>s;
int dat[maxn];
void build(int l, int r, int root){
int mid = (l+r)>>;
if(l==r){
dat[root] = mex[l];
return;
}
dat[root] = inf;
build(lson);
build(rson);
return;
}
void pushdown(int l, int r, int root){
if(l==r)return;
dat[lc]=min(dat[lc],dat[root]);
dat[rc]=min(dat[rc],dat[root]);
return;
}
int ask(int x, int l, int r, int root){
if(dat[root]!=inf)pushdown(l, r, root);
if(l==r)return dat[root];
int mid = (l+r)>>;
if(x<=mid)return ask(x,lson);
return ask(x,rson);
}
void update(int x, int y, int val, int l, int r, int root){
if(dat[root]!=inf)pushdown(l,r,root);
if(l==x&&y==r){
dat[root]=min(dat[root],val);return;
}
int mid = (l+r)>>;
if(y<=mid)update(x,y,val,lson);
else if(x>mid)update(x,y,val,rson);
else{
update(x,mid,val,lson);
update(mid+,y,val,rson);
}
return;
}
int ans[maxn];
int main(){
scanf("%d %d", &n, &q);
for(int i = ; i <= n; i++){
nx[i]=n+;
}
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
if(lst[a[i]]){
nx[lst[a[i]]]=i;
}
lst[a[i]]=i;
}
int tmp = ; for(int i = ; i <= n; i++){
s.insert(a[i]);
while(s.find(tmp)!=s.end()){
tmp++;
}
mex[i]=tmp;
}build(,n,);
for(int i = ; i <= q; i++){b[i].id=i;
scanf("%d %d", &b[i].l, &b[i].r);
}
sort(b+,b++q,cmp);
int p = ;
for(int i = ; i <= q; i++){
while(p<b[i].l){
//printf("\\\\%d\n",p);
update(p,nx[p]-,a[p],,n,);
//printf("---\n");
p++;
}
//printf("==%d %d %d\n",b[i].id,b[i].l,b[i].r);
//printf("--%d %d\n",i,ask(b[i].r,1,n,1));
ans[b[i].id]=ask(b[i].r,,n,);
}
for(int i = ; i <= q; i++){
printf("%d\n",ans[i]);
}
return ;
} /*
7 5
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7
*/

BZOJ 3339 Rmq Problem(离线+线段树+mex函数)的更多相关文章

  1. Bzoj 3339: Rmq Problem && Bzoj 3585: mex 莫队,树状数组,二分

    3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 833  Solved: 397[Submit][Status][D ...

  2. BZOJ 3339: Rmq Problem 莫队算法

    3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...

  3. BZOJ 3339: Rmq Problem

    3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1075  Solved: 549[Submit][Status][ ...

  4. [BZOJ3339] Rmq Problem(线段树)

    传送门 这个题的方法好像很多啊 1.莫队暴力 2.线段树 + 离线处理 先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + ...

  5. bzoj 3339 Rmq Problem / mex

    题目 我的树状数组怎么那么慢啊 就是一道水题,我们考虑一下对于一个区间\([l,r]\)什么样的数能被计算 显然需要对于一个\(j\),需要满足\(j<l\)且\(nxt_{j}>r\), ...

  6. BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)

    BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...

  7. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  9. BZOJ_2298_[HAOI2011]problem a_线段树

    BZOJ_2298_[HAOI2011]problem a_线段树 Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话( ...

随机推荐

  1. 利用SSH桥接访问服务器的Docker运行的MySql服务

    前情提要 docker的运用越来广泛,许多IT公司都已经将自己的服务跑在Docker上面.在安全性方面又做了一层防护.比如:跑在Docker上的Mysql不做外网端口映射,只能在linux服务器上或进 ...

  2. vue传值(父子传值,非父子传值)

    vue组件传值,分为父子传值和非父子传值,父子传值又分为父传子和子传父. 组件之间的传值,实现了数据的联动,是从操作Dom到操作数据一个跳转性的突破,在学习vue双向绑定原理之后, 这种观念就应该继续 ...

  3. Fabric1.4:手动启动 first-network 网络(一)

    注意:本文所使用的 fabric 版本为 v1.4.3,与其它版本的网络存在差异. 手动启动 first-network 网络系列分为三部分: 手动启动 first-network 网络(一) 手动启 ...

  4. CTO说|非容器化应用怎么玩多云?Kubernetes不管我们管啊

    Kubernetes已经成为容器编排系统的事实标准,是现在主流的跨云容器化应用操作系统. 但是,Kubernetes的目标并不是容器本身,而是承载其上的应用,本质上是为了解决(容器化)应用上云这个难题 ...

  5. Ubuntu18.04 一次性升级Python所有库

    pip是什么 pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 升级pip版本 默认Ubuntu自带的pip (pip 9.0.1)是基于Pytho ...

  6. 区间dp - 括号匹配并输出方案

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  7. 测试工具Fiddler(二)—— 入门使用

    Fiddler设置与安装证书 一.Fiddler常见设置 Options位置:Tools->Options 二.移动端连上Fiddler作为代理 注意:因为Charles也是默认8888端口,小 ...

  8. “Your build settings specify a provisioning profile with the UUID “”, however, no such provisioning profile was found”

    解决方法: 终端命令行输入下面语句,删除所有的Profilescd ~/Library/MobileDevice/Provisioning\ Profiles/rm *.mobileprovision

  9. 12.方法重载overload

    方法重载:overload 重载就是在一个类中,有相同的函数名称,但形参不同的函数 方法重载的规则: 方法名称必须相同 参数列表必须不同(个数不同.或类型不同.参数排列顺序不同等) 方法的返回值类型可 ...

  10. Matplotlib从兴趣到实践

    先看下Matplotlib实现的效果 是不是出现了也想敲一个的心动,那让我们一起来了解Matplotlib吧 Matplotlib安装 1.Windows系统安装Matplotlib 进入到cmd的命 ...