BZOJ 3223 SplayTree

https://www.lydsy.com/JudgeOnline/problem.php?id=3223

this is the first time I get in touch with the SplayTree

An miraculous Data Structure

For this problem

you should use the rotate

to make the intervals into a self-tree

and add a lazy lable to rotate the deep of the tree one by one

for the first time

I can't write this complex Data Structure by myself

so I use for reference from the VJ

code of AC:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int fa[N],l[N],r[N],num[N],rev[N];
int root=0;
void pushdown(int p){
    if(rev[p]){
        swap(l[p],r[p]);
        rev[l[p]]^=1;
        rev[r[p]]^=1;
        rev[p]^=1;
    }
}
void update(int p){
    num[p]=num[l[p]]+num[r[p]]+1;
}
void rotate(int x,int &k){
    int y=fa[x];int z=fa[y];
    int R=(r[y]==x);
    if(y==k) k=x;
    else{
        if(l[z]==y) l[z]=x;
        else r[z]=x;
    }
    if(R){
        fa[x]=z;fa[y]=x;fa[l[x]]=y;
        r[y]=l[x];
        l[x]=y;
    }else{
        fa[x]=z;fa[y]=x;fa[r[x]]=y;
        l[y]=r[x];
        r[x]=y;
    }
    update(x);
    update(y);
}
void splay(int x,int &k){
    while(x!=k){
        int y=fa[x];
        int z=fa[y];
        if(y!=k){
            if((x==l[y])^(y==l[z])) rotate(x,k);
            else rotate(y,k);
        }
        rotate(x,k);
    }
}
int find(int p,int x){
    pushdown(p);
    int L=l[p],R=r[p];
    if(num[L]+1==x){
        // splay(x,root);
        return p;
    }
    if(num[l[p]]>=x) return find(l[p],x);
    else return find(r[p],x-1-num[l[p]]);
}
void reverse(int L,int R){
    int x=find(root,L);
    int y=find(root,R+2);
    splay(x,root);
    splay(y,r[x]);
    rev[l[y]]^=1;
}
void Insert(int x){
    fa[root]=x;
    l[x]=root;
    num[x]=num[root]+1;
    root=x;
}
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n+2;++i){
        Insert(i);
    }
    while(m--){
        int a,b;
        cin>>a>>b;
        reverse(a,b);
    }
    for(int i=1;i<=n;++i){
        cout<<find(root,i+1)-1<<" ";
    }
    cout<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值