sgu 187 Twist and whirl - want to cheat 伸展树(splay)

38 篇文章 0 订阅
13 篇文章 0 订阅

        裸的区间翻转..直接写个splay就行...

        

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <memory.h>
#include <string>
#include <cstring>
using namespace std;

const int maxn=130030;
int pre[maxn],ch[maxn][2],size[maxn],key[maxn];
bool flip[maxn];
int n,m,tot,root;
struct splytree
{
    void pushup(int r)
    {
        if (r==0) return;
        size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
    }
    void pushdown(int r)
    {
        if (flip[r])
        {
            go_f(ch[r][0]);
            go_f(ch[r][1]);
            flip[r]=0;
        }
    }
    void go_f(int r)
    {
        if (!r) return;
        flip[r]^=1;
        swap(ch[r][0],ch[r][1]);
    }

    void rotate(int x,int kind)
    {
        int y=pre[x];
        pushdown(y);
        pushdown(x);
        ch[y][!kind]=ch[x][kind];
        pre[ch[x][kind]]=y;
        if (pre[y])
        {
            ch[pre[y]][ch[pre[y]][1]==y]=x;
        }
        pre[x]=pre[y];
        ch[x][kind]=y;
        pre[y]=x;
        pushup(y);
        pushup(x);
    }
    void splay(int x,int tgt)
    {
        pushdown(x);
        while (pre[x]!=tgt)
        {
            int y=pre[x];
            pushdown(pre[y]);
            pushdown(y);
            pushdown(x);
            if (pre[pre[x]]==tgt)
            {
                rotate(x,ch[pre[x]][0]==x);
            }
            else
            {
                int kind=ch[pre[y]][0]==y;
                if (ch[y][kind]==x)
                {
                    rotate(x,kind^1);
                    rotate(x,kind);
                }
                else
                {
                    rotate(y,kind);
                    rotate(x,kind);
                }
            }
        }
        pushup(x);
        if (tgt==0) root=x;
    }
    void select(int k,int tgt)
    {
        int rt=root;
        pushdown(rt);
        while (true)
        {
         if(k<=size[ch[rt][0]]) rt=ch[rt][0];
         else if (k==size[ch[rt][0]]+1) break;
         else k-=(size[ch[rt][0]]+1),rt=ch[rt][1];
         pushdown(rt);
        }
        splay(rt,tgt);
    }
    void newnode(int &r,int father,int k)
    {
        r=++tot;
        pre[r]=father;
        size[r]=1;
        flip[r]=0;
        key[r]=k;
        ch[r][0]=ch[r][1]=0;
    }
    void build(int l,int r,int &x,int rt)
    {
        if (l>r) return;
        int m=(l+r)>>1;
        newnode(x,rt,m);
        build(l,m-1,ch[x][0],x);
        build(m+1,r,ch[x][1],x);
        pushup(x);
    }
    void init()
    {
        tot=root=0;
        newnode(root,0,-1);
        newnode(ch[root][1],root,-1);
        build(1,n,ch[ch[root][1]][0],ch[root][1]);
        pushup(ch[root][1]);
        pushup(root);
    }
    void reverse(int posi,int num)
    {
        select(posi,0);
        select(posi+num+1,root);
        go_f(ch[ch[root][1]][0]);
//        int k=ch[ch[root][1]][0];
//        splay(k,0);
    }
    void print(int r)
    {
        if (r==0) return;
        pushdown(r);
        print(ch[r][0]);
        if (key[r]!=-1) printf("%d ",key[r]);
        print(ch[r][1]);
    }
}spt;
char tt[20],tt2[20];
int x,y;
int main()
{
//    freopen("in.txt","r",stdin);
        scanf("%d%d",&n,&m);
        spt.init();
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            spt.reverse(x,y-x+1);
        }
        spt.print(root);
        puts("");

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值