题意:

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. 交换机广播风暴,STP生成树协议,端口聚合

    交换机(工作在数据链路层)具有学习功能:     一台刚重启的交换机上的mac地址表为空,根据数据包的来源,目的地来学习MAC地址与端口的映射关系映射关系,对于MAC地址表之中已有的就不管了,对未知端 ...

  2. curl使用post方式访问Spring Cloud gateway报time out错误

    公司老的项目使用是php,要进行重构.其他团队使用php curl函数使用post方式调用Spring Cloud gateway 报time out错误. 但是使用postman测试是没有任何问题, ...

  3. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

  4. GPU图形绘制管线简介

    (阅读GPU+编程与CG+语言之阳春白雪下里巴人所得总结) GPU图形绘制管线是描述GPU渲染(把三维世界显示为屏幕上的二维图像)的流程,主要分为三个主要阶段应用程序阶段.几何阶段.光栅阶段. 1.应 ...

  5. python循环语句(while和for)

    循环语句分成两种,while循环 和 for循环 作用:可以使指定的代码块重复指定的次数 while循环: # 语法: # while 条件表达式 : # 代码块 # else : # 代码块 # 执 ...

  6. Spring学习记录1——IoC容器

    IoC容器 1.1  IoC概述 Ioc(Inverse of Control,控制反转)是Spring容器的内核.对于软件来说,即某一接口具体实现类的选择控制权从调用类中移除,转交给第三方决定,即由 ...

  7. Falco 进入 CNCF Incubator 项目 | 云原生生态周报 Vol. 35

    作者 | 王思宇.陈洁.敖小剑 业界要闻 Falco 进入 CNCF Incubator 项目 原于 2018 年 8 月进入 sandbox,旨在 Kubernetes 运行时环境下支持配置规则来加 ...

  8. request session

    例子 url = 'http://beanhome.com/user/login' header = { "Content-Type": 'application/json', & ...

  9. 高通量计算框架HTCondor(四)——案例准备

    目录 1. 正文 1.1. 任务划分 1.2. 任务程序 2. 相关 1. 正文 1.1. 任务划分 使用高通量计算第一步就是要针对密集运算任务做任务划分.将一个海量的.耗时的.耗资源的任务划分成合适 ...

  10. 深入学习MySQL 02 日志系统:bin log,redo log,undo log

    上一篇文章中,我们了解了一条查询语句的执行过程,按理说这篇应该讲一条更新语句的执行过程,但这个过程比较复杂,涉及到了好几个日志与事物,所以先梳理一下3个重要的日志,bin log(归档日志).redo ...