[Cerc2007]robotic sort

题目传送门

权限题就不放题目描述了

思路

for循环从 1 n,先输出 i 的大小,然后反转i i <script type="math/tex" id="MathJax-Element-132">i</script>的大小这一段。
注意要离散化。

代码

#include <cstdio>
#include <algorithm>

const int maxn=100000;

struct data
{
  int v,id;
};

data a[maxn+10];
int n;

struct splay_tree
{
  int fa[maxn+10],son[2][maxn+10],size[maxn+10],rev[maxn+10],root;

  inline int push_rev(int x)
  {
    rev[x]^=1;
    int t=son[0][x];
    son[0][x]=son[1][x];
    son[1][x]=t;
    return 0;
  }

  inline int pushdown(int x)
  {
    if(rev[x])
      {
    if(son[0][x])
      {
        push_rev(son[0][x]);
      }
    if(son[1][x])
      {
        push_rev(son[1][x]);
      }
    rev[x]=0;
      }
    return 0;
  }

  inline int updata(int x)
  {
    return size[x]=size[son[0][x]]+size[son[1][x]]+1;
  }

  inline int t(int x)
  {
    return son[1][fa[x]]==x;
  }

  inline int rotate(int x)
  {
    int f=fa[x];
    pushdown(f);
    pushdown(x);
    int k=t(x);
    if(fa[f])
      {
    son[t(f)][fa[f]]=x;
      }
    fa[x]=fa[f];
    if(son[!k][x])
      {
    fa[son[!k][x]]=f;
      }
    son[k][f]=son[!k][x];
    fa[f]=x;
    son[!k][x]=f;
    updata(f);
    updata(x);
    return 0;
  }

  inline int splay(int x,int c)
  {
    while(fa[x]!=c)
      {
    int f=fa[x];
    if(fa[f]==c)
      {
        rotate(x);
      }
    else if(t(x)==t(f))
      {
        rotate(f);
        rotate(x);
      }
    else
      {
        rotate(x);
        rotate(x);
      }
      }
    if(!c)
      {
    root=x;
      }
    return 0;
  }

  inline int getkth(int x)
  {
    int now=root;
    while(now)
      {
    pushdown(now);
    if(size[son[0][now]]+1==x)
      {
        return now;
      }
    else if(size[son[0][now]]+1<x)
      {
        x-=size[son[0][now]]+1;
        now=son[1][now];
      }
    else
      {
        now=son[0][now];
      }
      }
    return 0;
  }

  inline int getrank(int x)
  {
    splay(x,0);
    return size[son[0][x]]+1;
  }

  int build(int l,int r)
  {
    int mid=(l+r)>>1,x=a[mid].v;
    size[x]=r-l+1;
    if(l<=mid-1)
      {
    son[0][x]=build(l,mid-1);
    fa[son[0][x]]=x;
      }
    if(mid+1<=r)
      {
    son[1][x]=build(mid+1,r);
    fa[son[1][x]]=x;
      }
    return x;
  }

  inline int reverse(int l,int r)
  {
    if(l==1)
      {
    if(r==n)
      {
        push_rev(root);
      }
    else
      {
        int x=getkth(r+1);
        splay(x,0);
        push_rev(son[0][x]);
      }
      }
    else
      {
    int x=getkth(l-1);
    splay(x,0);
    if(r==n)
      {
        push_rev(son[1][x]);
      }
    else
      {
        int y=getkth(r+1);
        splay(y,x);
        push_rev(son[0][y]);
      }
      }
    return 0;
  }
};

splay_tree st;

bool cmpa(const data &b,const data &c)
{
  if(b.v==c.v)
    {
      return b.id<c.id;
    }
  return b.v<c.v;
}

bool cmpb(const data &b,const data &c)
{
  return b.id<c.id;
}

int main()
{
  scanf("%d",&n);
  for(register int i=1; i<=n; ++i)
    {
      scanf("%d",&a[i].v);
      a[i].id=i;
    }
  std::sort(a+1,a+n+1,cmpa);
  for(register int i=1; i<=n; ++i)
    {
      a[i].v=i;
    }
  std::sort(a+1,a+n+1,cmpb);
  st.build(1,n);
  st.root=a[(n+1)>>1].v;
  for(register int i=1; i<n; ++i)
    {
      int x=st.getrank(i);
      printf("%d ",x);
      st.reverse(i,x);
    }
  printf("%d",n);
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值