Descriptiongiven a sequence, the initial is empty. Now we insert a number from 1 to n into the sequence, inserting a number into a specific position at a time. Every time we insert a number, we want to know what is the longest ascending subsequence length? InputThe first line is an integer n, which indicates that we are going to insert 1 to n into the sequence, followed by n digits, the K number Xk, which means we insert K into position Xk (0<=xk<=k-1,1<=k<=n)Outputn rows, the line I represents the length of the longest ascending subsequence of the sequence after I insert the XI position. Sample Input3
0 0 2Sample Output1
1
2HINT
100% of Data n<=100000
Use Treap to maintain the queue information, and then do it directly. Worship KURIBOHG
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <algorithm>5 using namespacestd;6 Const intmaxn=1000001;7 structtreap8 {9 intch[2],dat,key,size,tms;Ten }TREAP[MAXN]; One intTot,root,cnt,a[maxn],dp[maxn],c[maxn],ans[maxn],n; AInlineintcmpintXinttar) - { - if(Treap[x].dat==tar)return-1; the return(Treap[x].dat<tar?)0:1); - } -InlinevoidMaintain (intx) - { +treap[x].size=treap[treap[x].ch[0]].size+treap[treap[x].ch[1]].size+1; - } +InlineintRotateint&x,intd) A { at intp=treap[x].ch[d^1]; -treap[x].ch[d^1]=Treap[p].ch[d]; -treap[p].ch[d]=x; - maintain (x); - maintain (p); -x=p; in } - voidInsint&x,intMinttar) to { + if(!x) - { thetot++; *Treap[tot].key=rand (); Treap[tot].size=1; treap[tot].tms=1; Treap[tot].dat=tar; x=tot; $ return;Panax Notoginseng } - intD; the if(m<=treap[treap[x].ch[0]].size) Ins (treap[x].ch[0],m,tar), d=0; + ElseINS (treap[x].ch[1],m-treap[treap[x].ch[0]].size-1, tar), d=1; A if(treap[treap[x].ch[d]].key>treap[x].key) Rotate (x,d^1); the maintain (x); + } - voidWorkintx) $ { $ if(treap[x].ch[0]) Work (treap[x].ch[0]); -a[++n]=Treap[x].dat; - if(treap[x].ch[1]) Work (treap[x].ch[1]); the } - intMainintargcChar*argv[])Wuyi { the inti,x; -scanf"%d",&n); Wu for(i=1, x;i<=n;i++) { -scanf"%d",&x); About ins (root,x,i); $ } -n=0; - Work (root); - for(i=1; i<=n;i++) Ac[i]=n+1; + for(i=1; i<=n;i++) the { - intSub=lower_bound (c,c+n+1, A[i])-C; $dp[i]=Sub; thec[dp[i]]=min (c[dp[i]],a[i]); theans[a[i]]=Dp[i]; the } the for(i=1; i<=n;i++) Ans[i]=max (ans[i],ans[i-1]); - for(i=1; i<=n;i++) printf ("%d\n", Ans[i]); in return 0; the}
BZOJ3173: [Tjoi2013] Longest ascending subsequence treap balance tree