NOI2004 郁闷的出纳员 SPLAY

郁闷的出纳员。我比他还郁闷,谢了将近两天,终于把第一到SPLAY写完了,之前一直用的treap但觉得splay的功能强大,有必要学一下。当然,当treap足够解决的时候还是用treap比较好,毕竟代码量要少点,不容易出错。

splay能够很好的对区间进行处理。

刚开始各种纠结,打算一个一个的删除,但由于判断加进了push_down 函数,程序直接爆掉,后来发现根本不能这么操作,因为这样会让程序无法终止。这次学到了。不要乱写down函数。加深了对splay的理解。

其实这道题目根本不用延时标记。

我们只需要用一个变量记录当前在公司的最低工资额(假如最初合同最低工资额为m  那么在n次调整后 最低的工资额应该为 m-a1-a2+s1+s2.....-an+sn)

注意当我们假如一个新数据的时候要对他的值进行修正。因为最小值判断是受前面的修改影响的。

我的程序一开始有个地方没写好。导致当要删除整棵树时出了问题。

注意点是 当一开始变低于要求值时。这个人数不算在离开公司的总人数中。

 

splay写的很挫。。。。1000多ms。。。。求改进。。。。貌似跑的太慢了点,我连把空间回收的函数都去掉了。。还是那么点,慢的没天理了,继续优化下

 

 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>

using namespace std;

#define MAXN 1000000

int next[MAXN];
struct nodes
{
    int ch[2],f;
    int key,size,col,w;
}node[MAXN];

void init()
{
    for(int i=0;i<MAXN-10;i++)
        next[i]=i+1;
}

int newnode(int key)
{
    int p=next[0];
    next[0]=next[p];
    node[p].key=key;
    node[p].ch[0]=node[p].ch[1]=node[p].f=0;
    node[p].size=node[p].w=1;
    return p;
}

void delnode(int p)
{
    next[p]=next[0];
    next[0]=p;
}

struct spt
{
    int root,sizes;

    void clear()
    {
        root=0;
    }
    void rotate(int x,int c)
    {
        int y=node[x].f;
        node[y].ch[!c]=node[x].ch[c];
        if(node[x].ch[c])
                node[node[x].ch[c]].f=y;
        node[x].f=node[y].f;
        if(node[y].f)
        {
            if(node[node[y].f].ch[0]==y)
                node[node[y].f].ch[0]=x;
            else
                node[node[y].f].ch[1]=x;
        }
        node[x].ch[c]=y;
        node[y].f=x;
        push_up(y);
        if(y==root) root=x;
    }
    void splay(int x,int f)
    {
        for(;node[x].f!=f;)
        {
            if(node[node[x].f].f==f)
            {
                if(node[node[x].f].ch[0]==x)
                    rotate(x,1);
                else
                    rotate(x,0);
            }
            else
            {
                int y=node[x].f;
                int z=node[y].f;
                if(node[z].ch[0]==y)
                {
                    if(node[y].ch[0]==x)
                        rotate(y,1),rotate(x,1);
                    else
                        rotate(x,0),rotate(x,1);
                }
                else
                {
                    if(node[y].ch[1]==x)
                        rotate(y,0),rotate(x,0);
                    else
                        rotate(x,1),rotate(x,0);
                }
            }
        }
        push_up(x);
        if(!f) root=x;
    }
    void insert(int key,int rt)
    {
        int p=rt,y=0;
        while(1)
        {
            if(!p)
            {
                p=newnode(key);
                node[p].f=y;
                if(y)
                    node[y].ch[key>node[y].key]=p;
                splay(p,0);
                break;
            }
            y=p;
            if(key==node[p].key)
            {
                node[p].w++;
                splay(p,0);
                break;
            }
            if(key<node[p].key)
                p=node[p].ch[0];
            else
                p=node[p].ch[1];
        }
    }
    int select(int k,int rt)
    {
        int tmp,t=rt;
        for(;;)
        {
            int l=node[node[t].ch[0]].size;
            if(k>l && k<=l+node[t].w) break;
            if(k<=l)
                t=node[t].ch[0];
            else
                k-=(l+node[t].w),t=node[t].ch[1];
        }
        splay(t,0);
        return t;
    }
    void push_up(int rt)
    {
        if(!rt)return;
        int l=node[rt].ch[0];
        int r=node[rt].ch[1];
        int ret=node[rt].w;
        if(l) ret+=node[l].size;
        if(r) ret+=node[r].size;
        node[rt].size=ret;
    }
    int find(int x,int p)
    {
        while(1)
        {
            if(!p)
                return 0;
            else
            {
                if(x==node[p].key)
                {
                    splay(p,0);
                    return p;
                }
                else
                {
                    if(x<node[p].key)
                        p=node[p].ch[0];
                    else
                        p=node[p].ch[1];
                }
            }
        }
        return 0;
    }
    int search(int a,int rt)
    {
        int p=rt,x=0x3FFFFFF;
        while(p)
        {
            if(node[p].key==a) return a;
            if(node[p].key>=a) x=min(x,node[p].key);
            p=node[p].ch[a>node[p].key];
         }
         return x;
    }

    int DelLift(int x)
    {
       int p=find(x,root),q=0;
       if(p)
       {
           if(node[p].ch[0])
              q=node[node[p].ch[0]].size;
           node[p].ch[0]=0;
           push_up(p);
       }
       else
       {
           q=node[root].size;
           root=0;
       }
       return q;
    }
};


void debug(int rt)
{
    if(!rt) return;
    debug(node[rt].ch[0]);
    printf("key:%d  size:%d col:%d\n",node[rt].key,node[rt].size,node[rt].col);
    debug(node[rt].ch[1]);
}


spt s1;

int main()
{
    char q[5];
    int n,m,ans,t,l;
    init();
    while(~scanf("%d%d",&n,&m))
    {
        s1.clear();
        l=m;ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s%d",q,&t);
            if(q[0]=='I')
            {
                if(t>=m)
                    s1.insert(t+l-m,s1.root);
            }
            if(q[0]=='S')
            {
                l+=t;
                ans+=s1.DelLift(s1.search(l,s1.root));
            }
            if(q[0]=='A')
                l-=t;
            if(q[0]=='F')
            {
                int p=-1;
                if(t<=node[s1.root].size)
                {
                    s1.select(node[s1.root].size-t+1,s1.root);
                    p=node[s1.root].key;
                    p=p-l+m;
                }
                printf("%d\n",p);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}




 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值