hdu 1890 splay 区间翻转

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000+100;
int n;
struct SplayTree
{
    int pre[maxn],ch[maxn][2],size[maxn],rev[maxn],root;
    void Update_Rev(int r)
    {
         rev[r]^=1;
    }
    void Push_Down(int r)
    {
        if(rev[r])
        {
            rev[ch[r][0]]^=1;
            rev[ch[r][1]]^=1;
            swap(ch[r][0],ch[r][1]);
            rev[r]=0;
        }
    }
    void Push_Up(int r)
    {
        size[r]=1+size[ch[r][0]]+size[ch[r][1]];
    }
    void Rotate(int x,int kind)
    {
        int y=pre[x];
        Push_Down(y);
        Push_Down(x);
        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;
        Push_Up(y);
    }
    void Splay(int r,int goal)
    {
        Push_Down(r);
        while(pre[r]!=goal)
        {
            if(pre[pre[r]]==goal)
            {
                Push_Down(pre[r]);
                Push_Down(r);
                Rotate(r,ch[pre[r]][0]==r);
            }
            else
            {
                Push_Down(pre[pre[r]]);
                Push_Down(pre[r]);
                Push_Down(r);
                int y=pre[r];
                int kind=ch[pre[y]][0]==y;
                if(ch[y][kind]==r)
                {
                    Rotate(r,kind^1);
                    Rotate(r,kind);
                }
                else
                {
                    Rotate(y,kind);
                    Rotate(r,kind);
                }
            }
        }
        Push_Up(r);
        if(goal==0) root=r;
    }
    void New_Node(int &r,int fa,int k)
    {
        r=k;//结点=下标
        pre[r]=fa;
        ch[r][0]=ch[r][1]=rev[r]=0;
        size[r]=1;
    }
    void Build(int &x,int l,int r,int fa)
    {
        if(l>r)return ;
        int mid=(l+r)>>1;
        New_Node(x,fa,mid);
        Build(ch[x][0],l,mid-1,x);
        Build(ch[x][1],mid+1,r,x);
        Push_Up(x);
    }
    void Init()
    {
        root=0;
        size[root]=0;
        Build(root,1,n,0);//无须先建2个节点
    }
    int Get_Max(int r)
    {
        Push_Down(r);
        while(ch[r][1])
        {
            r=ch[r][1];
            Push_Down(r);
        }
        return r;
    }
    void Remove()
    {
        if(ch[root][0]==0)
        {
            root=ch[root][1];
            pre[root]=0;
        }
        else
        {
            int m=Get_Max(ch[root][0]);
            Splay(m,root);
            ch[m][1]=ch[root][1];
            pre[ch[root][1]]=m;
            root=m;
            pre[m]=0;
            Push_Up(root);
        }
    }
}st;
struct Node
{
    int num;
    int id;
    bool operator<(const Node&b)const
    {
        if(num==b.num) return id<b.id;
        return num<b.num;
    }
}nodes[maxn];
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        st.Init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&nodes[i].num);
            nodes[i].id=i;
        }
        sort(nodes+1,nodes+1+n);
        for(int i=1;i<n;i++)
        {
            st.Splay(nodes[i].id,0);
            st.Update_Rev(st.ch[st.root][0]);
            printf("%d ",st.size[st.ch[st.root][0]]+i);
            st.Remove();
        }
        printf("%d\n",n);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值