hdu 3487 Play with Chain(Splay)

题目大意:

一个序列区间的反转操作

还有就是把[a,b] 这段序列拿出来,插到新序列的第C个的后面。


思路:

反转就和hdu 1890 一样的。

插入的话,先旋转到要的位置,拿出子序列。

然后旋转到C位置,插入子序列。


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define inf 0x3f3f3f3f
#define maxn 333333
#define keyTree (ch[ch[root][1]][0])

using namespace std;
typedef long long LL;
int S[maxn],que[maxn],ch[maxn][2],pre[maxn],siz[maxn];
int root,top1,top2;
int val[maxn],a[maxn];
int rev[maxn];
bool first;
void New(int &x,int PRE,int v)
{
    if(top2)x=S[--top2];
    else x=++top1;

    ch[x][0]=ch[x][1]=0;
    siz[x]=1;
    pre[x]=PRE;
    /*special*/
    rev[x]=0;
    val[x]=v;
}
void pushup(int x)/*special*/
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
void pushdown(int x)/*special*/
{
    if(rev[x])
    {
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
        rev[x]=0;
    }
}

void build(int &x,int s,int e,int f)
{
    if(s>e)return;
    int mid=(s+e)>>1;

    New(x,f,mid);

    if(s<mid)build(ch[x][0],s,mid-1,x);
    if(e>mid)build(ch[x][1],mid+1,e,x);
    pushup(x);
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(x);
    pushdown(y);
    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);
}
void Splay(int x,int goal)
{
    pushdown(x);
    while(pre[x]!=goal)
    {
        if(pre[pre[x]]==goal)
        Rotate(x,ch[pre[x]][0]==x);
        else
        {
            int y=pre[x];
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==x){
                Rotate(x,!kind);
                Rotate(x,kind);
            }
            else {
                Rotate(y,kind);
                Rotate(x,kind);
            }
        }
    }
    pushup(x);
    if(goal==0)root=x;
}

void RotateTo(int k,int goal)
{
    int r=root;
    pushdown(r);
    while(siz[ch[r][0]]!=k)
    {
        if(k<siz[ch[r][0]])
        {
            r=ch[r][0];
        }
        else
        {
            k-=siz[ch[r][0]]+1;
            r=ch[r][1];
        }
        pushdown(r);
    }
    Splay(r,goal);
}

void init(int n)/*special*/
{
    root=top1=top2=0;
    ch[0][0]=ch[0][1]=siz[0]=pre[0]=0;
    rev[0]=0;

    New(root,0,-1);
    New(ch[root][1],root,-1);

    siz[root]=2;

    build(keyTree,1,n,ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}
void print(int r)
{
    if(r)
    {
        pushdown(r);
        print(ch[r][0]);
        if(val[r]!=-1)
        {
            if(first)
            {
                printf("%d",val[r]);
                first=false;
            }
            else printf(" %d",val[r]);
        }
        print(ch[r][1]);
    }
}
void solve(int m)
{
    char str[10];
    int a,b;
    while(m--)
    {
        scanf("%s%d%d",str,&a,&b);
        if(str[0]=='F')
        {
            RotateTo(a-1,0);
            RotateTo(b+1,root);
            rev[keyTree]^=1;
        }
        else
        {
            int c;
            scanf("%d",&c);
            RotateTo(a-1,0);
            RotateTo(b+1,root);
            int tmp=keyTree;
            keyTree=0;
            pushup(ch[root][1]);
            pushup(root);
            RotateTo(c,0);
            RotateTo(c+1,root);
            keyTree=tmp;
            pre[tmp]=ch[root][1];
            pushup(ch[root][1]);
            pushup(root);
        }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        first=true;
        if(n<0&&m<0)break;
        init(n);
        solve(m);
        print(root);
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值