splay模板

#include<bits/stdc++.h>
using namespace std;
struct Node{
    Node *ch[2],*fa;
    int key,siz;
    bool flip;
    Node(int);
    int dir(int val){
        if(val==ch[0]->siz+1)   return -1;
        return val>ch[0]->siz+1;
    }
    int son(){
        if(fa->ch[0]==this)     return 0;
        if(fa->ch[1]==this)     return 1;
        return -1;
    }
    void maintain(){siz=ch[0]->siz+ch[1]->siz+1;}
    void pushdown(){
        if(flip){
            swap(ch[0],ch[1]);
            ch[0]->flip^=1;ch[1]->flip^=1;
            flip=false;
        }
    }
}*null,*root;
Node::Node(int _=-1){
    key=_;
    siz=null?1:0;
    flip=false;
    ch[0]=ch[1]=fa=null;
}
void buildtree(Node* &o,int l,int r){
    if(l>r) {o=null;return;}
    int mid=l+r>>1;
    o=new Node(mid);
    buildtree(o->ch[0],l,mid-1);o->ch[0]->fa=o;
    buildtree(o->ch[1],mid+1,r);o->ch[1]->fa=o;
    o->maintain();
}
void Rotate(Node *o,int dir){
    Node *tmp=o->ch[dir^1];
    o->ch[dir^1]=tmp->ch[dir];
    tmp->ch[dir]->fa=o;
    tmp->ch[dir]=o;
    o->maintain();  tmp->maintain();
    if(~o->son())  o->fa->ch[o->son()]=tmp;
    tmp->fa=o->fa;  o->fa=tmp;
}
void print(Node *o){
    if(o==null)     return;
    o->pushdown();
    print(o->ch[0]);
    printf("%d ",o->key);
    print(o->ch[1]);
}
void Pushdown(Node *o){
    if(o->fa!=null) Pushdown(o->fa);
    o->pushdown();
}
void Splay(Node *o){
    while(~o->son()){
        int dir=o->son();
        if(dir==o->fa->son())   Rotate(o->fa->fa,dir^1);
        Rotate(o->fa,dir^1);
    }
}
Node *Kth(Node *o,int k){
    o->pushdown();
    int dir=o->dir(k);
    if(dir==1)  k-=o->ch[0]->siz+1;
    if(dir==-1) return o;
    else        return Kth(o->ch[dir],k);
}
Node *Merge(Node *lhs,Node *rhs){
    if(lhs==null)   return rhs;
    if(rhs==null)   return lhs;
    Node *tmp=Kth(lhs,lhs->siz);
    Splay(tmp);
    tmp->ch[1]=rhs;
    rhs->fa=tmp;
    tmp->maintain();
    return tmp;
}
void Split(Node *org,int k,Node* &lhs,Node* &rhs){
    if(k==0){
        lhs=null;rhs=org;
        return;
    }
    else if(k==org->siz){
        lhs=org;rhs=null;
        return;
    }
    Node* tmp=Kth(org,k);
    Splay(tmp);
    lhs=tmp;rhs=tmp->ch[1];
    rhs->fa=null;lhs->ch[1]=null;lhs->maintain();
}
void Reverse(int l,int r){
    Node *lhs,*tmp,*mid,*rhs;
    Split(root,l-1,lhs,mid);
    Split(mid,r-l+1,mid,rhs);
    mid->flip^=1;
    root=Merge(Merge(lhs,mid),rhs);
}
int main(){
    null=new Node();
    null->ch[0]=null->ch[1]=null->fa=null;
    root=null;
    int n,m;
    scanf("%d%d",&n,&m);
    buildtree(root,1,n);
    for(int i=1;i<=m;++i){
        int l,r;
        scanf("%d%d",&l,&r);
        Reverse(l,r);
    }
    print(root);
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、5资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值