[BZOJ3223]文艺平衡树(平衡树splay)

题目描述

传送门

题解

平衡树的模板题。有区间操作,打标记。

代码

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

const int max_n=1e5+5;
const int INF=1e9;

int n,m,x,y;
int root,sz;
int ch[max_n][2],f[max_n],delta[max_n],size[max_n],key[max_n];
int a[max_n];

inline bool get(int x){
    return ch[ f[x] ][1]==x;
}

inline void update(int x){
    size[x]=size[ ch[x][0] ]+size[ ch[x][1] ]+1;
}

inline void pushdown(int x){
    if (x&&delta[x]){
        delta[ ch[x][0] ]^=1;
        delta[ ch[x][1] ]^=1;
        swap(ch[x][0],ch[x][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;

    f[x]=oldf;
    if (oldf)
      ch[oldf][ ch[oldf][1]==old ]=x;

    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==0) root=x;
}

inline int build(int l,int r,int fa){
    if (l>r) return 0;
    int mid=(l+r)>>1;

    int now=++sz;
    key[now]=a[mid]; f[now]=fa; delta[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 int find(int x){
    int now=root;
    while (1){
        pushdown(now);
        if (x<=size[ch[now][0]])
          now=ch[now][0];
        else{
            x-=size[ ch[now][0] ]+1;
            if (!x) return now;
            now=ch[now][1];
        }
    }
}

inline void print(int now){
    pushdown(now);
    if (ch[now][0]) print(ch[now][0]);
    if (key[now]!=-INF&&key[now]!=INF)
      printf("%d ",key[now]);
    if (ch[now][1]) print(ch[now][1]);
}

int main(){
    scanf("%d%d",&n,&m);
    a[1]=-INF; a[n+2]=INF;
    for (int i=1;i<=n;++i)
      a[i+1]=i;

    root=build(1,n+2,0);
    for (int i=1;i<=m;++i){
        scanf("%d%d",&x,&y);
        int aa=find(x);
        int bb=find(y+2);
        splay(aa,0);
        splay(bb,aa);
        delta[ ch[ ch[root][1] ][0] ]^=1;
    }
    print(root);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值