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 87 88 89 90 91 92 93 94
| #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int val[100005],ch[100005][2],fa[100005],siz[100005],maxx[100005],rt,cnt; inline void pushup(int x) { int L=ch[x][0],R=ch[x][1]; siz[x]=siz[L]+siz[R]+1; maxx[x]=max(val[x],max(maxx[L],maxx[R])); return; } inline void Rot(int x,int& f) { int y=fa[x],z=fa[y],L=(ch[y][0]!=x),R=(L^1); if(y==f)f=x; else { if(ch[z][0]==y)ch[z][0]=x; else ch[z][1]=x; } fa[x]=z;fa[y]=x; fa[ch[x][R]]=y; ch[y][L]=ch[x][R]; ch[x][R]=y; pushup(y); pushup(x); return; } inline void Splay(int x,int& f) { while(x!=f) { int y=fa[x],z=fa[y]; if(y!=f) { if(ch[z][0]==y^ch[y][0]==x)Rot(x,f); else Rot(y,f); } Rot(x,f); } return; } inline int Find(int x,int k) { int L=ch[x][0],R=ch[x][1]; if(siz[L]+1==k)return x; if(siz[L]+1>k)return Find(L,k); return Find(R,k-siz[L]-1); } inline void Insert(int x,int value) { int L=Find(rt,x),R=Find(rt,x+1); Splay(L,rt); Splay(R,ch[L][1]); fa[++cnt]=R; val[cnt]=maxx[cnt]=value; siz[cnt]=1; ch[R][0]=cnt; pushup(R); pushup(L); return; } inline int Getmax(int x,int y) { int L=Find(rt,x-1),R=Find(rt,y+1); Splay(L,rt); Splay(R,ch[L][1]); return maxx[ch[R][0]]; } inline void Init(void) { rt=1;cnt=2; val[1]=val[2]=-0x3FFFFFFF; ch[1][0]=2;fa[2]=1; siz[1]=2;siz[2]=1; return; } int main(void) { Init(); int i,n,x,ans,Ans=0; scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",&x); ans=Getmax(2,x+1)+1; printf("%d\n",Ans=max(Ans,ans)); Insert(x+1,ans); } return 0; }
|