splay的翻转应用

#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cstdio>
#include<string>//完整头文件
#include<ctime>
using namespace std;
const int maxn=100010;
struct one{
    int val,lo;
    bool operator <(const one &a)const{
        if(val==a.val) return lo<a.lo;
        return val<a.val;
    }
}ones[maxn];
int n;
int root;
int rev[maxn],pre[maxn],size[maxn];//翻转lazy 父节点 节点个数
int tree[maxn][2];

void pushup(int x)
{
    size[x]=size[tree[x][1]]+size[tree[x][0]]+1;
}//向上更新节点数

void update_rev(int x)
{
    if (!x) return;
    swap(tree[x][0],tree[x][1]);
    rev[x]^=1;
}//翻转x的儿子

void pushdown(int x)
{
    if(rev[x])
    update_rev(tree[x][0]);
    update_rev(tree[x][1]);
    rev[x]=0;
}//翻转下移

void rotate (int x,int c)
{
    int y=pre[x];
    pushdown(y);
    pushdown(x);
    tree[y][!c]=tree[x][c];
    pre[tree[x][c]]=y;
    if(pre[y])
      tree[pre[y]][tree[pre[y]][1]==y]=x;
      pre[x]=pre[y];
      tree[x][c]=y;
      pre[y]=x;
      pushup(y);
}//单自旋


void splay(int x,int goal)
{
  pushdown(x);
  while(pre[x]!=goal)
  {
      if(pre[pre[x]]==goal)
      {
        pushdown(pre[x]);pushdown(x);
        rotate(x,tree[pre[x]][0]==x);
      }
      else {
          pushdown(pre[pre[x]]);
          pushdown(pre[x]);
          pushdown(x);
          int y=pre[x];
          int c=(tree[pre[y]][0]==y);
          if(tree[y][c]==x)
          {
              rotate(x,!c);
              rotate(x,c);
          }
          else {
              rotate(y,c);
              rotate(x,c);
          }
      }
  }
  pushup(x);
  if(goal==0)
  root =x;
}//除非到用才下移标记,不然保持,重复翻转即抵消//

int get_max(int x)
{
    pushdown(x);
    while(tree[x][1])
    {
        x=tree[x][1];
        pushdown(x);
    }
    return x;
}

void del_root()
{
    if(tree[root][0]==0)
    {
        root=tree[root][1];
        pre[root]=0;
    }
    else{
        int m=get_max(tree[root][0]);
        splay(m,root);
        tree[m][1]=tree[root][1];
        pre[tree[root][1]]=m;
        root=m;
        pre[root]=0;
        pushup(root);
    }
}

void newone(int &x,int fa,int val)
{
    x=val;
    pre[x]=fa;
    size[x]=1;
    rev[x]=0;
    tree[x][0]=tree[x][1]=0;
}

void build(int &x,int l,int r,int fa)
{
  if(l>r)
  return ;
  else {
      int mid=(r+l)>>1;
      newone(x,fa,mid);
      build(tree[x][0],l,mid-1,x);
      build(tree[x][1],mid+1,r,x);
      pushup(x);
  }
}//中序//
void init()
{
    root=0;
    tree[root][0]=tree[root][1]=pre[root]=size[root]=0;
    build(root,1,n,0);
}
int main ()
{
    while(scanf("%d",&n)!=EOF,n)
    {
        init();
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&ones[i].val);
            ones[i].lo=i;
        }
        sort(ones+1,ones+n+1);
        for(int i=1;i<n;++i)
        {
            splay(ones[i].lo,0);
            update_rev(tree[root][0]);
            printf("%d ",i+size[tree[root][0]]);
            del_root();
        }
        printf("%d\n",n);
    }
    return 0;
}

用数组建树,一维坐标为值即位置,数值大小仅起排序作用,

二叉排序树 递归翻转 标记翻转(重复翻转为一致)若用到则往下传递 然后取反

记得向上更新数组个数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值