Rmq Problem / mex

It has a length n array {a1, a2, ..., an}. m times query, asking each a natural number within the minimum interval there have been no.

To 100% of the data: 1 <= n, m <= 200000,0 <= ai <= 10 ^ 9,1 <= l <= r <= n

answer

The position of each maintenance interval prefix each number with the last occurrence of the Chairman of the tree, if there are a number of queries appear position <l then he can choose.

There are some questions that this idea interval

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=200005;
const int maxm=7000005;
const int oo=1000000000;
int n,m,cnt;
int root[maxn],ls[maxm],rs[maxm],mi[maxm];

template<class T>inline void read(T &x){
    x=0;int f=0;char ch=getchar();
    while(!isdigit(ch)) {f|=(ch=='-');ch=getchar();}
    while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x= f ? -x : x ;
}

void copy(int x,int y){
    ls[x]=ls[y];
    rs[x]=rs[y];
    mi[x]=mi[y];
}

void update(int rt){
    mi[rt]=min(mi[ls[rt]],mi[rs[rt]]);
}

int modify(int rt,int l,int r,int pos,int val){
    int t=++cnt;
    copy(t,rt);
    if(l==r){
        mi[t]=val;
        return t;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) ls[t]=modify(ls[rt],l,mid,pos,val);
    else rs[t]=modify(rs[rt],mid+1,r,pos,val);
    update(t);
    return t;
}

int query(int rt,int l,int r,int val){
    if(l==r) return l;
    int mid=(l+r)>>1;
    if(mi[ls[rt]]<val) return query(ls[rt],l,mid,val);
    else return query(rs[rt],mid+1,r,val);
}

int main(){
    read(n);read(m);
    for(int i=1;i<=n;i++) {
        int x;read(x);
        root[i]=modify(root[i-1],0,oo,x,i);
    }
    while(m--){
        int l,r;
        read(l);read(r);
        printf("%d\n",query(root[r],0,oo,l));
    }
}
mex

 

Guess you like

Origin www.cnblogs.com/sto324/p/11627716.html