[bzoj 1861--ZJOI2006]书架

124 篇文章 2 订阅
1 篇文章 0 订阅

小T有一个很大的书柜。这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列。她用1到n的正整数给每本书都编了号。
小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本。由于这些书太有吸引力了,所以她看完后常常会忘记原来是放在书柜的什么位置。不过小T的记忆力是非常好的,所以每次放书的时候至少能够将那本书放在拿出来时的位置附近,比如说她拿的时候这本书上面有X本书,那么放回去时这本书上面就只可能有X-1、X或X+1本书。
当然也有特殊情况,比如在看书的时候突然电话响了或者有朋友来访。这时候粗心的小T会随手把书放在书柜里所有书的最上面或者最下面,然后转身离开。
久而久之,小T的书柜里的书的顺序就会越来越乱,找到特定的编号的书就变得越来越困难。于是她想请你帮她编写一个图书管理程序,处理她看书时的一些操作,以及回答她的两个提问:(1)编号为X的书在书柜的什么位置;(2)从上到下第i本书的编号是多少。

这道题算是splay第一题吧,第一,第二操作需要把原来的点给删掉,然后再加进去,但是在树上的编号是不可以变的。第三个只是单纯的交换,第四,第五个是模版里的基本操作,所以就不用说了。这道题我感觉新奇的地方在于要开个数组来记录编号为i在树上的位置,以此来方便查询(我一开始没注意到数据范围这么小)。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int root;
struct node
{
    int d,c,f,son[2];
}tr[81000];int len,ys[81000];
void update(int x)
{
    int lc=tr[x].son[0],rc=tr[x].son[1];
    tr[x].c=tr[lc].c+tr[rc].c+1;
}
void add(int d,int f,int id)
{
    tr[id].d=d;tr[id].f=f;
    tr[id].son[0]=tr[id].son[1]=0;
}
void rotate(int x,int w)
{
    int f=tr[x].f,ff=tr[f].f;
    int R,r;
    R=f,r=tr[x].son[w];
    tr[R].son[1-w]=r;
    if(r!=0)tr[r].f=R;
    R=ff,r=x;
    if(tr[R].son[0]==f)tr[R].son[0]=r;
    else tr[R].son[1]=r;
    tr[r].f=R;
    R=x,r=f;
    tr[R].son[w]=r;
    tr[r].f=R;
    update(f);update(x);
}
void splay(int x,int rt)
{
    while(tr[x].f!=rt)
    {
        int f=tr[x].f,ff=tr[f].f;
        if(ff==rt)
        {
            if(tr[f].son[0]==x)rotate(x,1);
            else rotate(x,0);
        }
        else
        {
            if(tr[f].son[0]==x && tr[ff].son[0]==f)rotate(f,1),rotate(x,1);
            if(tr[f].son[1]==x && tr[ff].son[1]==f)rotate(f,0),rotate(x,0);
            if(tr[f].son[0]==x && tr[ff].son[1]==f)rotate(x,1),rotate(x,0);
            if(tr[f].son[1]==x && tr[ff].son[0]==f)rotate(x,0),rotate(x,1);
        }
    }
    if(rt==0)root=x;
}
void del(int d)
{
    int x=ys[d];splay(x,0);
    if(tr[x].son[0]==0 && tr[x].son[1]==0){root=len=0;return ;}
    if(tr[x].son[0]==0 && tr[x].son[1]!=0){root=tr[x].son[1];tr[root].f=0;return ;}
    if(tr[x].son[0]!=0 && tr[x].son[1]==0){root=tr[x].son[0];tr[root].f=0;return ;}
    int p=tr[x].son[0];
    while(tr[p].son[1]!=0)p=tr[p].son[1];splay(p,x);
    int R=p,r=tr[x].son[1];
    tr[R].son[1]=r;
    tr[r].f=R;
    root=R;tr[root].f=0;
    update(R);
}
void Top(int d)
{
    int x=ys[d];
    int p=root;
    while(tr[p].son[0]!=0)p=tr[p].son[0];splay(p,0);
    tr[p].son[0]=x;
    add(d,p,x);
    update(x);
    update(p);
    splay(x,0);
}
void Bottom(int d)
{
    int x=ys[d];
    int p=root;
    while(tr[p].son[1]!=0)p=tr[p].son[1];splay(p,0);
    tr[p].son[1]=x;
    add(d,p,x);
    update(x);
    update(p);
    splay(x,0);
}
void ins(int d,int f)
{
    add(d,f,++len),update(len);
    if(f==0)root=len;
    else
    {
        tr[f].son[1]=len;
        update(len),update(f);
        splay(len,0);
    }
    ys[d]=len;
}
void Insert(int d,int k)
{
    int x=ys[d];
    splay(x,0);
    int p;
    if(k==-1)
    {
        p=tr[x].son[0];
        while(tr[p].son[1]!=0)p=tr[p].son[1];splay(p,x);
    }
    else
    {
        p=tr[x].son[1];
        while(tr[p].son[0]!=0)p=tr[p].son[0];splay(p,x);
    }
    int wy1=tr[x].d,wy2=tr[p].d;
    swap(tr[x].d,tr[p].d);
    ys[wy1]=p;ys[wy2]=x;
}
int Ask(int d)
{
    int x=ys[d];splay(x,0);
    return tr[tr[x].son[0]].c;
}
int Query(int k)
{
    int x=root;
    while(1)
    {
        int lc=tr[x].son[0],rc=tr[x].son[1];
        if(k<=tr[lc].c)x=lc;
        else if(tr[lc].c+1<k)x=rc,k-=tr[lc].c+1;
        else break;
    }
    return tr[x].d;
}
char ss[10];
int main()
{
    int n,m,last=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        ins(x,last);
        last=ys[x];
    }
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%s%d",ss+1,&x);
        if(ss[1]=='T')del(x),Top(x);
        if(ss[1]=='B')del(x),Bottom(x); 
        if(ss[1]=='I')
        {
            int k;
            scanf("%d",&k);
            if(k!=0)Insert(x,k);
        }
        if(ss[1]=='A')printf("%d\n",Ask(x));
        if(ss[1]=='Q')printf("%d\n",Query(x));
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值