1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define Turnoff std::ios::sync_with_stdio(false)
const int Max=2e5+5; const int Mod=998244353; const int inf=0x3f3f3f3f;
#define __DEBUG__ #ifdef __DEBUG__ #define DeBug(format, ...) \ { \ fprintf(stdout, "\[%s:%d] \" format " ", \ __FUNCTION__ , __LINE__, ##__VA_ARGS__); \ } #else #define DeBug(format,...) #endif
/*
*/ class SegTree{ public: #define mid ((L+R)>>1) #define Leftson now<<1 #define Rightson now<<1|1 struct segtree{int L,R,pos;}; segtree tree[Max<<2]; void push_up(int now){ tree[now].pos=min(tree[Leftson].pos,tree[Rightson].pos); return; } void build(int L,int R,int now=1){ tree[now]={L,R,0}; //cout<<L<<" "<<R<<endl; if(L==R)return; //int mid=(L+R)>>1; build(L,mid,Leftson); build(mid+1,R,Rightson); push_up(now); return; } void updata(int qL,int qR,int val,int now=1){ if(qL<=tree[now].L&&qR>=tree[now].R){ tree[now].pos=val; return; } if(qL<=tree[Leftson].R)updata(qL,qR,val,Leftson); if(qR>=tree[Rightson].L)updata(qL,qR,val,Rightson); push_up(now);return; } int query(int qL,int qR,int now=1){ if(qL>qR)return inf; if(tree[now].R<=qR&&tree[now].L>=qL)return tree[now].pos; int minnpos=inf; if(tree[Leftson].R>=qL)minnpos=min(minnpos,query(qL,qR,Leftson)); if(tree[Rightson].L<=qR)minnpos=min(minnpos,query(qL,qR,Rightson)); push_up(now); return minnpos; } }Val; bool vis[Max]; int arr[Max],last[Max]; int main(){ //Turnoff; int n;cin>>n; Val.build(1,n); for(int i=1;i<=n;i++)cin>>arr[i]; for(int i=1;i<=n;i++){ if(arr[i]!=1)vis[1]=1; if(arr[i]>1&&Val.query(1,arr[i]-1)>last[arr[i]])vis[arr[i]]=1; last[arr[i]]=i; Val.updata(arr[i],arr[i],i); } for(int i=2;i<=n+1;i++){ if(Val.query(1,i-1)>last[i])vis[i]=1; } int Mex=1; while(vis[Mex])Mex++; cout<<Mex<<endl; return 0; }
|