[Bzoj3173] increased the longest sequence

With f [i] represents the longest sequence i rise value at the end of this, consider the effect produced by insertion:
1. Because intervening sequence from small to large, and therefore does not change the existing value of f
2 at this point the value of f All that position before taking his f max + 1 again, but because the sequence of operations to support the insert is required to maintain a balanced tree

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 100005
 4 #define s(p) ch[k][p]
 5 int V,n,r,k,s,ans,a[N],ma[N],ra[N],sz[N],ch[N][2];
 6 void New(int k,int x){
 7     a[k]=ma[k]=x;
 8     ra[k]=rand();
 9     sz[k]=1;
10 }
11 void up(int k){
12     sz[k]=sz[s(0)]+sz[s(1)]+1;
13     ma[k]=max(a[k],max(ma[s(0)],ma[s(1)]));
14 }
15 void rotate(int &k,int u,int p){
16     s(p)=ch[u][p^1];
17     ch[u][p^1]=k;
18     up(k);
19     up(k=u);
20 }
21 void add(int &k,int x){
22     if (!k){
23         New(k=++V,s);
24         return;
25     }
26     bool p=(sz[s(0)]<x);
27     add(s(p),x-p*(sz[s(0)]+1));
28     up(k);
29     if (ra[s(p)]<ra[k])rotate(k,s(p),p);
30 }
31 int query(int k,int x){
32     if (!k)return 0;
33     if (x<=sz[s(0)])return query(s(0),x);
34     return max(query(s(1),x-sz[s(0)]-1),max(ma[s(0)],a[k]));
35 }
36 int main(){
37     srand(time(0));
38     scanf("%d",&n);
39     for(int i=1;i<=n;i++){
40         scanf("%d",&k);
41         printf("%d\n", year = max (years, s = query (r, k) + 1 ));
42          add (r, k);
43      }
 44 }
View Code

 

Guess you like

Origin www.cnblogs.com/PYWBKTDA/p/11416173.html