zoukankan      html  css  js  c++  java
  • BZOJ 3223 Splay区间翻转

    思路:
    区间翻转的裸题 终于tm理解splay了……

    //By SiriusRen
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 155555
    int n,m,root,size[N],ch[N][2],fa[N],rev[N],v[N];
    void push_up(int x){size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}
    int build(int l,int r){
        if(l>r)return 0;
        if(l==r){size[l]=1,v[l]=l-1;return l;}
        int mid=(l+r)>>1;v[mid]=mid-1;
        ch[mid][0]=build(l,mid-1);fa[ch[mid][0]]=mid;
        ch[mid][1]=build(mid+1,r);fa[ch[mid][1]]=mid;
        push_up(mid);
        return mid;
    }
    void rotate(int p){
        int q=fa[p],y=fa[q],f=(ch[q][1]==p);
        ch[q][f]=ch[p][!f],fa[ch[q][f]]=q;
        ch[p][!f]=q;fa[q]=p;fa[p]=y;
        if(y){
            if(ch[y][0]==q)ch[y][0]=p;
            else ch[y][1]=p;
        }push_up(q);
    }
    void Splay(int x,int tp){
        for(int y;y=fa[x];rotate(x)){
            if(y==tp)break;
            if(fa[y]!=tp){
                if((ch[y][0]==x)^(ch[fa[y]][0]==y))rotate(x);
                else rotate(y);
            }
        }push_up(x);
        if(!tp)root=x;
    }
    void push_down(int x){
        if(rev[x]){
            rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
            swap(ch[x][0],ch[x][1]);
            rev[x]=0;
        }
    }
    int find(int x,int num){
        push_down(x);
        int sz=size[ch[x][0]];
        if(sz+1==num)return x;
        else if(sz+1>num)return find(ch[x][0],num);
        else return find(ch[x][1],num-sz-1);
    }
    void reverse(int l,int r){
        int y=find(root,l);Splay(y,0);
        Splay(find(root,r+2),y);
        rev[ch[ch[root][1]][0]]^=1;
    }
    void dfs(int x){
        push_down(x);
        if(ch[x][0])dfs(ch[x][0]);
        if(v[x]&&v[x]!=n+1)printf("%d ",v[x]);
        if(ch[x][1])dfs(ch[x][1]);
    }
    int main(){
        scanf("%d%d",&n,&m);
        root=build(1,n+2);
        for(int i=1;i<=m;i++){
            int l,r;
            scanf("%d%d",&l,&r);
            reverse(l,r);
        }
        dfs(root);
    }

    这里写图片描述

  • 相关阅读:
    Pro ASP.NET Core MVC2
    vscode
    git命令使用
    单行函数
    过滤和排序
    oracle基本查询
    斐波那契数列的递归实现
    队列的顺序存储结构
    队列的链式存储结构
    折半查找法
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532120.html
Copyright © 2011-2022 走看看