[BZOJ 3173] [TJOI 2013] 最长上升子序列(splay)

根据题意很容易得到最原始的做法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=100005;
int n,m,f[N];
int main(){
    scanf("%d",&n);
    int x;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        for(int j=i-1;j>=x+1;j--)
        	f[j+1]=f[j];
		int maxn=0;
		for(int j=0;j<=x;j++)
			maxn=max(maxn,f[j]);
        f[x+1]=maxn+1;
		maxn=0;
		for(int j=0;j<=i;j++)
			maxn=max(maxn,f[j]);
		printf("%d\n",maxn);
    }
    return 0;
} 

接着,用splay,fhq_treap…这些数据结构实现在 f[ ] 数组中插入一个数、求区间最大值的操作就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int N=100005*2;
const int inf=0x3f3f3f3f;
int fa[N],ch[N][2],key[N],sz[N],maxn[N];
int root,tot1;
int s[N],tot2;
int n;
void NewNode(int &x,int pre,int k){
    if(tot2) x=s[tot2--];
    else x=++tot1;
    ch[x][0]=ch[x][1]=0;
    fa[x]=pre;
    sz[x]=1;
    key[x]=maxn[x]=k;
}
void push_up(int x){
    sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
    maxn[x]=key[x];
    if(ch[x][0]) maxn[x]=max(maxn[x],maxn[ch[x][0]]);
    if(ch[x][1]) maxn[x]=max(maxn[x],maxn[ch[x][1]]);
}
void rotate(int x,int d){
    int y=fa[x];
    ch[y][!d]=ch[x][d];
    fa[ch[x][d]]=y;
    if(fa[y]) ch[fa[y]][ch[fa[y]][1]==y]=x;
    fa[x]=fa[y];
    ch[x][d]=y;
    fa[y]=x;
    push_up(y);
}
void splay(int x,int goal){
    while(fa[x]!=goal){
        if(fa[fa[x]]==goal)
            rotate(x,ch[fa[x]][0]==x);
        else{
            int y=fa[x];
            int d=(ch[fa[y]][0]==y);
            if(ch[y][d]==x){
                rotate(x,!d);
                rotate(x,d);
            }
            else{
                rotate(y,d);
                rotate(x,d);
            }
        }
    }
    push_up(x);
    if(goal==0) root=x;
}
int get_kth(int x,int k){
    int t=sz[ch[x][0]]+1;
    if(t==k) return x;
    if(t>k) return get_kth(ch[x][0],k);
    else get_kth(ch[x][1],k-t);
}
void update_insert(int x,int p){
    splay(get_kth(root,x+1),0);
    splay(get_kth(root,x+2),root);
    NewNode(Key_value,ch[root][1],p);
    push_up(ch[root][1]);
    push_up(root);
}
int query_maxn(int l,int r){
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    return maxn[Key_value];
}
int main(){
	NewNode(root,0,inf);
    NewNode(ch[root][1],root,inf);
    NewNode(Key_value,ch[root][1],0);
    scanf("%d",&n);
	int x,p;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);x++;
        p=query_maxn(1,x)+1;
        update_insert(x,p);
        printf("%d\n",query_maxn(1,i+1));
    }
    return 0;
}

(加1减1什么的真的快把我调疯了)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值