bzoj 3173: [Tjoi2013]最长上升子序列(splay)

3173: [Tjoi2013]最长上升子序列

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1315   Solved: 682
[ Submit][ Status][ Discuss]

Description

给定一个序列,初始为空。现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置。每插入一个数字,我们都想知道此时最长上升子序列长度是多少?

Input

第一行一个整数N,表示我们要将1到N插入序列中,接下是N个数字,第k个数字Xk,表示我们将k插入到位置Xk(0<=Xk<=k-1,1<=k<=N)

Output

N行,第i行表示i插入Xi位置后序列的最长上升子序列的长度是多少。

Sample Input

3
0 0 2

Sample Output

1
1
2

HINT

100%的数据 n<=100000

Source

[ Submit][ Status][ Discuss]

题解:splay维护区间最大值,对于splay中的每个节点,维护两个值,一个是到当前点的最长上升子序列长度,一个是他以及他的子树中最长上升子序列的最大值,更新当前点的最长上升子序列长度时,因为插入时是按照从小到大的方式插入的,所以要用他前驱中的最大值来更新答案,因为他是序列中最大的所以不会影响他后面点的答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100003
using namespace std;
int n,root;
int key[N],size[N],ch[N][3],fa[N],sz,maxn[N],g[N];
int get(int x)
{
	return ch[fa[x]][1]==x;
}
void update(int x)
{
	size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    maxn[x]=g[x];
	maxn[x]=max(maxn[x],maxn[ch[x][0]]);
	maxn[x]=max(maxn[x],maxn[ch[x][1]]);
	if (key[x]==1000000000||key[x]==0)  maxn[x]=0,g[x]=0;
}
void rotate(int x)
{
	int y=fa[x]; int z=fa[y]; int which=get(x);
	if (z)  ch[z][ch[z][1]==y]=x;
	fa[x]=z; ch[y][which]=ch[x][which^1]; fa[ch[y][which]]=y;
	ch[x][which^1]=y; fa[y]=x; 
	update(y); update(x);
}
void splay(int x,int tar)
{
	for (int f;(f=fa[x])!=tar;rotate(x))  
     if (fa[f]!=tar)  
      rotate(get(x)==get(f)?f:x);  
    if (!tar)  
     root=x;  
}
int find(int x)
{
	int now=root; 
	while (true)
	{
		if (x<=size[ch[now][0]])
		 now=ch[now][0];
		else
		 {
		 	int tmp=size[ch[now][0]]+1;
		 	if (tmp==x) return now;
		 	x-=tmp;
		 	now=ch[now][1];
		 }
	}
}
int main()
{
	scanf("%d",&n);
	root=++sz; key[sz]=1000000000; size[root]=2; fa[root]=0; ch[root][1]=++sz;
	key[sz]=0; size[sz]=1; ch[sz][1]=ch[sz][0]=0; fa[sz]=root;
	for (int i=1;i<=n;i++)
	 {
	 	int x; scanf("%d",&x);
	 	x++;
	 	int aa=find(x);
	 	int bb=find(x+1); 
	 	splay(aa,0); splay(bb,aa);
	 	int t=ch[root][1]; 
	 	ch[t][0]=++sz; fa[sz]=t; key[sz]=i; ch[sz][1]=ch[sz][0]=0;
	 	size[sz]=1;  splay(t,0); 
	 	splay(sz,0); 
		g[sz]=maxn[ch[root][0]]+1;  update(sz);
		printf("%d\n",maxn[sz]);
	 }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值