题意:
思路:
注意到一个很关键的条件,每次插入
i
i
i,而
i
i
i是递增的,也就是说插入
i
i
i之后只会从前面的最大值转移过来,所以我们现在只需要维护插入操作即可,这个显然可以用平衡树来维护。
让后就是一个裸的
f
h
q
−
t
r
e
a
p
fhq-treap
fhq−treap了,我们需要维护一个
d
p
dp
dp数组,
d
p
[
i
]
dp[i]
dp[i]表示以
i
i
i个数结尾的最长上升子序列,对于平衡树中的每个节点都维护一个他的位置
p
o
s
pos
pos,当
p
u
s
h
u
p
pushup
pushup的时候
m
a
x
(
d
p
[
t
r
[
u
]
.
p
o
s
]
,
m
a
x
(
t
r
[
t
r
[
u
]
.
l
]
.
v
a
l
,
t
r
[
t
r
[
u
]
.
r
]
.
v
a
l
)
)
max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val))
max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val))转移即可。注意
d
p
[
t
r
[
u
]
.
p
o
s
]
dp[tr[u].pos]
dp[tr[u].pos]不能用
t
r
[
u
]
.
v
a
l
tr[u].val
tr[u].val代替,因为他的值在子树分裂之前存的是子树中最大值,分裂后不一定是这个,有可能变小,所以需要一个
d
p
dp
dp数组存下来当前位置的答案。
附带
d
e
b
u
g
debug
debug代码。
// Problem: P4309 [TJOI2013]最长上升子序列
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4309
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,root,x,y,z;
int tot;
int dp[N];
struct Node {
int l,r;
int id,size,rank,fa,val;
int pos;
}tr[N];
void pushup(int u) {
tr[u].size=tr[tr[u].l].size+tr[tr[u].r].size+1;
tr[u].val=max(dp[tr[u].pos],max(tr[tr[u].l].val,tr[tr[u].r].val));
}
int newnode(int v,int i) {
int u=++tot;
tr[u].l=tr[u].r=0;
tr[u].rank=rand(); tr[u].size=1;
tr[u].val=v;
tr[u].pos=i;
dp[i]=v;
return u;
}
void split(int u,int k,int &x,int &y) {
if(!u) { x=y=0; return; }
if(k<=tr[tr[u].l].size) y=u,split(tr[u].l,k,x,tr[u].l);
else x=u,split(tr[u].r,k-tr[tr[u].l].size-1,tr[u].r,y);
pushup(u);
}
int merge(int u,int v) {
if(!u||!v) return v+u;
if(tr[u].rank<tr[v].rank) {
tr[u].r=merge(tr[u].r,v);
pushup(u);
return u;
}
else {
tr[v].l=merge(u,tr[v].l);
pushup(v);
return v;
}
}
void dfs(int u) {
if(!u) return;
dfs(tr[u].l);
printf("%d %d\n",u,tr[u].val);
dfs(tr[u].r);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%d",&n);
for(int i=1;i<=n;i++) {
int pos; scanf("%d",&pos);
split(root,pos,x,y);
int val=tr[x].val;
//cout<<i<<' '<<val<<endl;
//cout<<"**"<<i<<' '<<val<<endl;
//dfs(x);
root=merge(merge(x,newnode(val+1,i)),y);
printf("%d\n",tr[root].val);
//db;
}
return 0;
}
/*
*/