[BZOJ1251]序列终结者(splay)

题目描述

传送门

题解

splay模板题。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int max_n=5e4+5;
const int INF=2e9;

int n,m,opt,l,r,v,root,sz,ans;
int a[max_n],num[max_n];
int f[max_n],ch[max_n][2],key[max_n],maxn[max_n],delta[max_n],add[max_n],size[max_n];

inline int get(int x){return ch[f[x]][1]==x;}
inline void update(int x){
    if (x){
        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]]);
        size[x]=1;
        if (ch[x][0]) size[x]+=size[ch[x][0]];
        if (ch[x][1]) size[x]+=size[ch[x][1]];
    }
}
inline int build(int l,int r,int fa){
    if (l>r) return 0;
    int mid=(l+r)>>1;
    int now=++sz;
    key[now]=maxn[now]=a[mid]; size[now]=1; f[now]=fa; delta[now]=0; add[now]=0;
    int lch=build(l,mid-1,now);
    int rch=build(mid+1,r,now);
    ch[now][0]=lch; ch[now][1]=rch;
    update(now);
    return now;
}
inline void pushdown(int x){
    if (add[x]){
        if (ch[x][0]) add[ch[x][0]]+=add[x],maxn[ch[x][0]]+=add[x],key[ch[x][0]]+=add[x];
        if (ch[x][1]) add[ch[x][1]]+=add[x],maxn[ch[x][1]]+=add[x],key[ch[x][1]]+=add[x];
        add[x]=0;
    }
    if (delta[x]){
        swap(ch[x][0],ch[x][1]);
        if (ch[x][0]) delta[ch[x][0]]^=1;
        if (ch[x][1]) delta[ch[x][1]]^=1;
        delta[x]=0;
    }
}
inline void rotate(int x){
    pushdown(f[x]);
    pushdown(x);
    int old=f[x],oldf=f[old],which=get(x);
    ch[old][which]=ch[x][which^1];
    f[ch[old][which]]=old;
    ch[x][which^1]=old;
    f[old]=x;
    if (oldf) ch[oldf][ch[oldf][1]==old]=x;
    f[x]=oldf;
    update(old);
    update(x);
}
inline void splay(int x,int tar){
    for (int fa;(fa=f[x])!=tar;rotate(x))
      if (f[fa]!=tar)
        rotate((get(x)==get(fa))?fa:x);
    if (!tar) root=x;
}
inline int find(int x){
    int now=root;
    while (1){
        pushdown(now);
        if (ch[now][0]&&size[ch[now][0]]>=x) now=ch[now][0];
        else{
            int tmp=1;
            if (ch[now][0]) tmp+=size[ch[now][0]];
            if (x<=tmp) return now;
            x-=tmp; now=ch[now][1];
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    a[1]=-INF; a[n+2]=INF;
    root=build(1,n+2,0);
    for (int i=1;i<=m;++i){
        scanf("%d",&opt);
        scanf("%d%d",&l,&r);l++;r++;
        int aa=find(l-1);
        int bb=find(r+1);
        splay(aa,0);
        splay(bb,aa);

        if (opt==1){
            scanf("%d",&v);
            maxn[ch[ch[root][1]][0]]+=v;
            key[ch[ch[root][1]][0]]+=v;
            add[ch[ch[root][1]][0]]+=v;
            update(ch[root][1]);
            update(root);
        }
        if (opt==2){
            delta[ch[ch[root][1]][0]]^=1;
        }
        if (opt==3){
            ans=maxn[ch[ch[root][1]][0]];
            printf("%d\n",ans);
        }
    }
}

总结

注意key值和maxn值一起更新,并且update的时候应该取三个max。
不能直接记某个点在树中的编号,因为牵扯到区间反转。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值