UVA 11922 Permutation Transformer && SGU 187. Twist and whirl - want to cheat(伸展树)

UVA 11922   Permutation Transformer

题意:最开始有个序列{1,2,3,……,n},有m个操作,每个操作要把[a,b]这个区间翻转,然后添加到序列尾部。最后输出最终序列。

思路:伸展树第二题。。感觉对伸展树明白了一些,就是各种操作写的时候容易写错啊,转来转去有些晕。实现翻转可以把第a-1个元素旋转到根,把b+1旋转到根的右结点,此时b+1的左结点即为所要操作的区间,首先把这个子树删掉,翻转可以加个标记,表示这个区间被翻转过,然后把序列最后一个元素旋转到根,然后把这课子树插进去就行了。本来想写两个函数来着,以后说不定可以用,但是想不出来好方法……只好偷个懒~还有,伸展树真是好难debug……


代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int pre[maxn],ch[maxn][2],size[maxn],val[maxn];
int filp[maxn],tot,root;
int n,q;
void Newnode(int &rt,int fa,int v)
{
    rt=++tot;
    pre[rt]=fa;
    ch[rt][0]=ch[rt][1]=0;
    val[rt]=v;
    size[rt]=1;
    filp[rt]=0;
}
inline void PushUp(int rt)
{
    size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+1;
}
void PushDown(int rt)
{
    if(filp[rt])
    {
        filp[ch[rt][0]]^=1;
        filp[ch[rt][1]]^=1;
        swap(ch[rt][0],ch[rt][1]);
        filp[rt]=0;
    }
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    PushDown(x);PushDown(y);
    ch[y][kind^1]=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 rt,int goal)
{
    PushDown(rt);
    while(pre[rt]!=goal)
    {
        int y=pre[rt];
        if(pre[y]==goal)
            Rotate(rt,ch[y][0]==rt);
        else
        {
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==rt)
            {
                Rotate(rt,kind^1);
                Rotate(rt,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(rt,kind);
            }
        }
    }
    PushUp(rt);
    if(goal==0) root=rt;
}
void RotateTo(int k,int goal)
{
    int rt=root;
    PushDown(rt);
    while(k!=size[ch[rt][0]])
    {
        if(k<size[ch[rt][0]])
            rt=ch[rt][0];
        else
        {
            k-=(size[ch[rt][0]]+1);
            rt=ch[rt][1];
        }
        PushDown(rt);
    }
    Splay(rt,goal);
}

void trans(int l,int r)
{
    RotateTo(l-1,0);
    RotateTo(r+1,root);
    int x=ch[root][1];
    int rt=ch[x][0];
    int k=size[rt];
    PushDown(rt);
    ch[x][0]=0;
    PushUp(x);PushUp(root);
    RotateTo(n-k,0);
    x=ch[root][1];
    ch[x][0]=rt;
    pre[rt]=x;
    filp[rt]^=1;
    PushUp(x);PushUp(root);
}
void build(int l,int r,int & rt,int fa)
{
    if(l>r) return ;
    int m=(l+r)>>1;
    Newnode(rt,fa,m);
    build(l,m-1,ch[rt][0],rt);
    build(m+1,r,ch[rt][1],rt);
    PushUp(rt);
}
void Init()
{
    pre[0]=ch[0][0]=ch[0][1]=0;
    size[0]=val[0]=filp[0]=0;
    tot=root=0;
    Newnode(root,0,0);
    Newnode(ch[root][1],root,0);
    build(1,n,ch[ch[root][1]][0],ch[root][1]);
    PushUp(ch[root][1]);
    PushUp(root);
}
void dfs(int rt)
{
    PushDown(rt);
    if(ch[rt][0]) dfs(ch[rt][0]);
    if(val[rt]) printf("%d\n",val[rt]);
    if(ch[rt][1]) dfs(ch[rt][1]);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(~scanf("%d%d",&n,&q))
    {
        Init();
        int l,r;
        while(q--)
        {
            scanf("%d%d",&l,&r);
            trans(l,r);
        }
        dfs(root);
    }
    return 0;
}



SGU 187. Twist and whirl - want to cheat


题意:和上面的题差不多,而且更简单,只存在翻转操作。。。

思路:同上。。。


代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=130000+10;
int pre[maxn],ch[maxn][2],size[maxn],val[maxn];
int filp[maxn],tot,root;
int n,q;
void Newnode(int &rt,int fa,int v)
{
    rt=++tot;
    pre[rt]=fa;
    ch[rt][0]=ch[rt][1]=0;
    val[rt]=v;
    size[rt]=1;
    filp[rt]=0;
}
inline void PushUp(int rt)
{
    size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+1;
}
void PushDown(int rt)
{
    if(filp[rt])
    {
        filp[ch[rt][0]]^=1;
        filp[ch[rt][1]]^=1;
        swap(ch[rt][0],ch[rt][1]);
        filp[rt]=0;
    }
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    PushDown(x);PushDown(y);
    ch[y][kind^1]=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 rt,int goal)
{
    PushDown(rt);
    while(pre[rt]!=goal)
    {
        int y=pre[rt];
        if(pre[y]==goal)
            Rotate(rt,ch[y][0]==rt);
        else
        {
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==rt)
            {
                Rotate(rt,kind^1);
                Rotate(rt,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(rt,kind);
            }
        }
    }
    PushUp(rt);
    if(goal==0) root=rt;
}
void RotateTo(int k,int goal)
{
    int rt=root;
    PushDown(rt);
    while(k!=size[ch[rt][0]])
    {
        if(k<size[ch[rt][0]])
            rt=ch[rt][0];
        else
        {
            k-=(size[ch[rt][0]]+1);
            rt=ch[rt][1];
        }
        PushDown(rt);
    }
    Splay(rt,goal);
}

void trans(int l,int r)
{
    RotateTo(l-1,0);
    RotateTo(r+1,root);
    int x=ch[root][1];
    int rt=ch[x][0];
    PushDown(rt);
    filp[rt]^=1;
}
void build(int l,int r,int & rt,int fa)
{
    if(l>r) return ;
    int m=(l+r)>>1;
    Newnode(rt,fa,m);
    build(l,m-1,ch[rt][0],rt);
    build(m+1,r,ch[rt][1],rt);
    PushUp(rt);
}
void Init()
{
    pre[0]=ch[0][0]=ch[0][1]=0;
    size[0]=val[0]=filp[0]=0;
    tot=root=0;
    Newnode(root,0,0);
    Newnode(ch[root][1],root,0);
    build(1,n,ch[ch[root][1]][0],ch[root][1]);
    PushUp(ch[root][1]);
    PushUp(root);
}
void dfs(int rt)
{
    PushDown(rt);
    if(ch[rt][0]) dfs(ch[rt][0]);
    if(val[rt]) printf("%d ",val[rt]);
    if(ch[rt][1]) dfs(ch[rt][1]);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d%d",&n,&q);
    Init();
    int l,r;
    while(q--)
    {
        scanf("%d%d",&l,&r);
        trans(l,r);
    }
    dfs(root);
    printf("\n");
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值