bzoj#3585. mex

题意:区间mex
题解:主席树维护,按权值插入,维护区间最小值,第x颗线段树,区间l,r表示l到r在1到x出现最后的最早一个是哪个位置

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=200000+10,inf=0x3f3f3f3f;

int a[N],root[N*20],ls[N*20],rs[N*20],mi[N*20],cnt;
void pushup(int o){mi[o]=min(mi[ls[o]],mi[rs[o]]);}
void update(int last,int &o,int pos,int v,int l,int r)
{
    o=++cnt;
    mi[o]=mi[last];ls[o]=ls[last];rs[o]=rs[last];
    if(l==r){mi[o]=v;return ;}
    int m=(l+r)>>1;
    if(pos<=m)update(ls[last],ls[o],pos,v,l,m);
    else update(rs[last],rs[o],pos,v,m+1,r);
    pushup(o);
}
int query(int o,int v,int l,int r)
{
    if(l==r)return l;
    int m=(l+r)>>1;
    if(mi[ls[o]]>=v)return query(rs[o],v,m+1,r);
    else return query(ls[o],v,l,m);
}
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);a[i]++;
        update(root[i-1],root[i],a[i],i,1,n);
    }
    while(m--)
    {
        int l,r;scanf("%d%d",&l,&r);
        printf("%d\n",query(root[r],l,1,n)-1);
    }
    return 0;
}
/********************
5 5
2 1 0 2 1
********************/
posted @ 2018-12-15 15:01  walfy  阅读(170)  评论(0编辑  收藏  举报