[bzoj4919] 大根堆

[bzoj4919] 大根堆


暴力dp都会吧,多加一维记最大值,然后我们线段树合并来优化转移就行了。

  • 代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int NN=6e6+5;
int fa[N],val[N],root[N];
int lch[NN],rch[NN],mx[NN],lzy[NN],cnt,n;
inline void pushdown(int x){
    if(!x||!lzy[x])return;
    mx[x]+=lzy[x];
    if(lch[x])lzy[lch[x]]+=lzy[x];
    if(rch[x])lzy[rch[x]]+=lzy[x];
    lzy[x]=0;
}
inline int merge(int rt1,int rt2,int plul,int plur){
    pushdown(rt1);pushdown(rt2);
    if(!rt1||!rt2){
        if(!rt1) lzy[rt2]=plul;
        else lzy[rt1]=plur;
        return rt1|rt2;
    }
    if(!lch[rt1]&&!lch[rt2]&&!rch[rt1]&&!rch[rt2]){
        mx[rt1]=max(max(mx[rt1]+plur,mx[rt2]+plul),mx[rt1]+mx[rt2]);
        return rt1;
    }
    pushdown(lch[rt1]);pushdown(lch[rt2]);
    int nwplul=max(plul,mx[lch[rt1]]);
    int nwplur=max(plur,mx[lch[rt2]]);
    lch[rt1]=merge(lch[rt1],lch[rt2],plul,plur);
    rch[rt1]=merge(rch[rt1],rch[rt2],nwplul,nwplur);
    pushdown(lch[rt1]);pushdown(rch[rt1]);
    mx[rt1]=max(mx[lch[rt1]],mx[rch[rt1]]);
    return rt1;
}

inline void update(int &rt,int l,int r,int val,int sum){
    int mid=(l+r)>>1;
    if(!rt)rt=++cnt;
    pushdown(rt);
    if(l==r){
        mx[rt]=max(mx[rt],sum+1);
        return;
    }
    if(val>mid){
        pushdown(lch[rt]);
        sum=max(mx[lch[rt]],sum);
        update(rch[rt],mid+1,r,val,sum);
    }
    else update(lch[rt],l,mid,val,sum);
    pushdown(lch[rt]);pushdown(rch[rt]);
    mx[rt]=max(mx[lch[rt]],mx[rch[rt]]);
}
pair<int,int> a[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&val[i],&fa[i]);
        a[i]=make_pair(val[i],i);
    }
    sort(a+1,a+n+1);
    int cont=0;
    for(int i=1;i<=n;i++){
        if(i==1||a[i].first!=a[i-1].first)++cont;
        val[a[i].second]=cont;
    }
    for(int i=n;i;i--){
        update(root[i],1,cont,val[i],0);
        pushdown(root[i]);
        if(fa[i])root[fa[i]]=merge(root[fa[i]],root[i],0,0);
    }
    pushdown(root[1]);
    printf("%d\n",mx[root[1]]);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值