bzoj3173 最长上升自序列 块状链表

15 篇文章 0 订阅
1 篇文章 0 订阅

题意:动态插入数,求每次插完后的最长上升自序列长度。(N<=100000)
动态插入部分 块状链表
块状链表:
split:分裂 O(sqrt(n))
merge:合并 O(sqrt(n))
insert:插入 具体做法是找到对应的块O(sqrt(n)),在该块内找到对应的点O(sqrt(n))split,merge(first,now),merge(now,second)
maintain:整理 当两个相邻块sz和大于sqrt(n)时,merge,据说合并次数少,所以maintain操作也是O(sqrt(n))的。
dp部分参考上一篇题解吧……

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 400
#define inf 0x7fffffff
using namespace std;
struct node{
    int a[N*2],sz;
    node *next;
};
node *tmp1,*tmp2,*head;
int n,k,pos,num;
int a[N*N],d[N*N],ans[N*N];
void merge(node *t1,node *t2){
    for(int i=t1->sz+1;i<=t1->sz+t2->sz;i++)
        t1->a[i]=t2->a[i-t1->sz];
    t1->sz+=t2->sz;
    t1->next=t2->next;
    delete t2;
}
void split(node *t1,int pos){
    tmp2=new node;
    for(int i=pos+1;i<=t1->sz;i++)
        tmp2->a[i-pos]=t1->a[i],t1->a[i]=0;
    tmp2->next=t1->next;t1->next=tmp2;
    tmp2->sz=t1->sz-pos;t1->sz=pos;
}
void insert(int x,int pos){
    if(head==NULL){
        tmp1=new node;
        head=tmp1;
        tmp1->a[1]=x;
        tmp1->next=NULL;
        tmp1->sz=1;
        return;
    }
    tmp1=head;
    while(tmp1->sz<pos){
        pos-=tmp1->sz;
        tmp1=tmp1->next;
    }
    split(tmp1,pos);
    tmp2=new node;
    tmp2->a[1]=x;
    tmp2->sz=1;
    tmp2->next=tmp1->next;
    tmp1->next=tmp2;
    tmp1=head;
    while(tmp1->next!=NULL){
        if(tmp1->sz+tmp1->next->sz<=k) merge(tmp1,tmp1->next); 
        else tmp1=tmp1->next;
    }
}
void getans(){
    memset(d,127,sizeof(d));
    d[0]=-inf;d[1]=a[1];ans[a[1]]=1;
    int len=1;
    for(int i=2;i<=n;i++){
        int pos=upper_bound(d,d+len,a[i])-d;
        //cout<<pos<<endl;
        if(a[i]>d[len]) d[++len]=a[i],ans[a[i]]=len;
        else d[pos]=min(d[pos],a[i]),ans[a[i]]=pos;
    }
    for(int i=1;i<=n;i++) ans[i]=max(ans[i],ans[i-1]);
    for(int i=1;i<=n;i++) printf("%d\n",ans[i]);
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%d",&n);k=(int)sqrt(n);
    for(int i=1;i<=n;i++)
        scanf("%d",&pos),insert(i,pos);
    tmp1=head;
    while(tmp1!=NULL){
        for(int i=1;i<=tmp1->sz;i++) a[++num]=tmp1->a[i];
        tmp1=tmp1->next;
    }
    getans();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值