实在是太弱了。。。。

考虑离线,从mex[l,r]向mex[l,r+1]转移,显然是没啥东西可以记录的。。。

从mex[l,r]向mex[l+1,r]转移,记x=mex[l,r],如果[l+1,r]不出现a[l]的话,那么mex[l+1,r]=min(mex[l,r],a[l]).

有了这个性质的话,就可以考虑离线了。先预处理一遍to[]和mex[],to[i]表示i的后面下一个出现a[i]的数组下标,mex[i]表示mex[1,i]。

把询问按左端点升序排序,对于当前的左端点为1的排序,可以直接输出它的mex值。

当左端点向右移一位的时候,mex[2,j]=min(mex[1,j],a[1]).(j<to[1]).

如果暴力修改的话还是会超时的。

很幸运mex数组是单调非递减的。那么我们可以用线段树把区间[2,j]上的值取个min。用tag思想的话很容易做到logn。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Node{int l, r, id;}node[N];
int seg[N<<], a[N], mex[N], vis[N], to[N], now, tag[N<<], ans[N], res; bool comp(Node a, Node b){return a.l<b.l;}
void init(int p, int l, int r){
if (l<r) {
int mid=(l+r)>>;
init(lch); init(rch); return ;
}
seg[p]=mex[l];
}
void push_down(int p, int l, int r){
if (tag[p]==-) return ;
if (l==r) seg[p]=min(seg[p],tag[p]);
else {
tag[p<<]=(tag[p<<]==-?tag[p]:min(tag[p],tag[p<<]));
tag[p<<|]=(tag[p<<|]==-?tag[p]:min(tag[p],tag[p<<|]));
}
tag[p]=-;
}
void update(int p, int l, int r, int L, int R, int val){
if (L>r||R<l) return ;
push_down(p,l,r);
if (L<=l&&R>=r) tag[p]=val;
else {
int mid=(l+r)>>;
update(lch,L,R,val); update(rch,L,R,val);
}
}
void query(int p, int l, int r, int L){
if (L>r||L<l) return ;
push_down(p,l,r);
if (L==r&&L==l) res=seg[p];
else {
int mid=(l+r)>>;
query(lch,L); query(rch,L);
}
}
int main ()
{
int n, q;
mem(tag,-);
n=Scan(); q=Scan();
FOR(i,,n) {
a[i]=Scan();
if (vis[a[i]]) to[vis[a[i]]]=i;
vis[a[i]]=i;
while (vis[now]) ++now;
mex[i]=now;
}
FOR(i,,n) if (!to[i]) to[i]=n+;
FOR(i,,q) node[i].l=Scan(), node[i].r=Scan(), node[i].id=i;
sort(node+,node+q+,comp);
now=;
init(,,n);
FOR(i,,q) {
while (now!=node[i].l) {
if (now+<=to[now]-) update(,,n,now+,to[now]-,a[now]);
++now;
}
query(,,n,node[i].r);
ans[node[i].id]=res;
}
FOR(i,,q) printf("%d\n",ans[i]);
return ;
}

HUAS 1483 mex(离线+线段树)的更多相关文章

  1. BZOJ 3585: mex( 离线 + 线段树 )

    离线, 询问排序. 先处理出1~i的答案, 这样可以回答左端点为1的询问.完成后就用seq(1)将1到它下一次出现的位置前更新. 不断这样转移就OK了 ------------------------ ...

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

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

  3. bzoj2333 离线 + 线段树

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

  4. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  5. HUAS 1483 mex(莫队算法)

    考虑莫队算法,对于区间减小的情况,可以O(1)解决.对于区间增加的情况,可能需要O(n)解决.好在数据不卡莫队. 1200ms过了. 离线+线段树 760ms过了. # include <cst ...

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

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

  7. BZOJ 3339 Rmq Problem(离线+线段树+mex函数)

    题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...

  8. 【BZOJ】3339: Rmq Problem & 3585: mex(线段树+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3585 好神的题. 但是!!!!!!!!!!!!!!我线段树现在要开8倍空间才能过!!!!!!!!!! ...

  9. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

随机推荐

  1. 5 Ways to Prevent the 300ms Click Delay on Mobile Devices

    http://www.sitepoint.com/5-ways-prevent-300ms-click-delay-mobile-devices/

  2. sas的使用

    1.建表 /*************************************/ /* create the second input data set */ /*************** ...

  3. 北京Uber优步司机奖励政策(2月27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 北京Uber优步司机奖励政策(1月19日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 成都Uber优步司机奖励政策(4月4日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 青岛Uber优步司机奖励政策(9月14日~9月20日)

    由于上周银行系统升级,工资延后 9/14-9/20奖励细则 滴滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...

  7. iOS性能调优工具

    总结: 三类工具 基础工具 (NSLog的方式记录运行时间.) 性能工具.检测各个部分的性能表现,找出性能瓶颈 内存工具.检查内存正确性和内存使用效率 性能工具: 可以衡量CPU的使用,时间的消耗,电 ...

  8. nested class 例子

    #include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclo ...

  9. [转帖]localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...

  10. Use GitHub Desktop to get GitHub projects

    Find the project's https git file in the home page of the project. e.g. https://github.com/PrismLibr ...