BZOJ 3173: [Tjoi2013]最長上升子序列
題解:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100005;
int rnd[N],ls[N],rs[N],siz[N],root;
int n,a[N],pos[N],len,now,lis[N],da[N],c[N],ans;
void Pushup(int x)
{
siz[x]=siz[ls[x]]+siz[rs[x]]+1;
}
void Lx(int &x)
{
int t=rs[x];
rs[x]=ls[t],ls[t]=x;
Pushup(x),Pushup(t);
x=t;
}
void Rx(int &x)
{
int t=ls[x];
ls[x]=rs[t],rs[t]=x;
Pushup(x),Pushup(t);
x=t;
}
int cnt;
void Insert(int &x,int v)
{
if(!x)
{
x=++cnt;
siz[x]=1;
rnd[x]=rand();
return;
}
siz[x]++;
if(v<=siz[ls[x]])
{
Insert(ls[x],v);
if(rnd[ls[x]]>rnd[x]) Rx(x);
}
else
{
Insert(rs[x],v-siz[ls[x]]-1);
if(rnd[rs[x]]>rnd[x]) Lx(x);
}
}
void dfs(int x)
{
if(!x) return;
dfs(ls[x]);
a[++now]=x,pos[x]=now;
dfs(rs[x]);
}
void Modify(int x,int v)
{
for(;x<=n;x+=(x&-x)) c[x]=max(c[x],v);
}
int Query(int x)
{
int tmp=0;
for(;x;x-=(x&-x)) tmp=max(tmp,c[x]);
return tmp;
}
int main()
{
scanf("%d",&n);
int v;
for(int i=1;i<=n;i++)
{
scanf("%d",&v);
Insert(root,v);
}
dfs(root);
for(int i=1;i<=n;i++)
{
a[pos[i]]=Query(pos[i]-1)+1;
Modify(pos[i],a[pos[i]]);
ans=max(ans,a[pos[i]]);
printf("%d\n",ans);
}
}
Description
Input
Output
Sample Input
0 0 2
Sample Output
1
2
HINT
100%的數據 n<=100000