题目
每次加入的是最大值。
那么我们只需要求出插入位置前面的最长上升子序列长度即可。
用平衡树实现插入和区间求Max。
AC Code(Treap真短):
#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
int Ran(){
static int G = 3;
return G = (3ll * G) % 998244353;
}
int ch[maxn][2],val[maxn],siz[maxn],Max[maxn],pri[maxn];
int rt,tot;
void upd(int x){
Max[x] = val[x];
if(ch[x][0]) Max[x] = max(Max[x] , Max[ch[x][0]]);
if(ch[x][1]) Max[x] = max(Max[x] , Max[ch[x][1]]);
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}
void rot(int &y,int c){
int x = ch[y][c];
ch[y][c] = ch[x][!c] , ch[x][!c] = y;
upd(y) , upd(x);
y = x;
}
void ins(int &x,int pos,int v){
if(!x){
val[x = ++tot] = v , siz[x] = 1 , pri[x] = Ran();
Max[x] = v;
return;
}
int d = (pos>=(siz[ch[x][0]]+1));
ins(ch[x][d],pos-d*(siz[ch[x][0]]+1),v);
if(pri[ch[x][d]] < pri[x]) rot(x,d);
upd(x);
}
int Query(int x,int rk){
if(!x || !rk) return 0;
if(siz[ch[x][0]] >= rk) return Query(ch[x][0],rk);
return max(Max[ch[x][0]],max(val[x],Query(ch[x][1],rk-siz[ch[x][0]]-1)));
}
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
int tmp = Query(rt,x)+1;
ins(rt,x,tmp);
printf("%d\n",Max[rt]);
}
}