hdu 4557 非诚勿扰(平衡树方法)

  • Post author:
  • Post category:其他



hdu 4557 非诚勿扰

本题数据小,可暴力水过,这里用SBT练下手

Find相当于求后继,key保存响应的用来比较的数据

#include<stdio.h>
#include<string.h>
#define MAXN 1005
struct person
{
    char name[17];
    int key,kth;
    bool operator < (const person &a) const
    {
        if(key==a.key) return kth<a.kth;
        return key<a.key;
    }
    bool operator > (const person &a) const
    {
        if(key==a.key) return kth>a.kth;
        return key>a.key;
    }
    bool operator == (const person &a) const
    {
        return key==a.key&&kth==a.kth;
    }
};
struct node
{
    int l,r;
    person key;
    int size;
}t[MAXN];
int top,root;
void lRotate(int &p)
{
    int rch=t[p].r;
    t[p].r=t[rch].l;
    t[rch].l=p;
    
    t[rch].size=t[p].size;
    t[p].size=t[t[p].l].size+t[t[p].r].size+1;
    
    p=rch;
}
void rRotate(int &p)
{
    int lch=t[p].l;
    t[p].l=t[lch].r;
    t[lch].r=p;
    
    t[lch].size=t[p].size;
    t[p].size=t[t[p].l].size+t[t[p].r].size+1;
    
    p=lch;
}
void maintain(int &p,bool flag)
{
    if(!flag)
    {
        if(t[t[t[p].l].l].size>t[t[p].r].size)
            rRotate(p);
        else if(t[t[t[p].l].r].size>t[t[p].r].size)
        {
            lRotate(t[p].l);
            rRotate(p);
        }
        else return ;
    }
    else
    {
        if(t[t[t[p].r].r].size>t[t[p].l].size)
            lRotate(p);
        else if(t[t[t[p].r].l].size>t[t[p].l].size)
        {
            rRotate(t[p].r);
            lRotate(p);
        }
        else return ;
    }
    maintain(t[p].l, false);
    maintain(t[p].r, true);
    maintain(p, false);
    maintain(p, true);
}
void insert(int &p,person &key)
{
    if(!p)
    {
        p=++top;
        t[p].l=t[p].r=0;
        t[p].size=1;
        t[p].key=key;
        return ;
    }
    else
    {
        t[p].size++;
        if(key<t[p].key) insert(t[p].l,key);
        else insert(t[p].r,key);
        maintain(p, !(key<t[p].key));
    }
}
person remove(int &p,person &key)
{
    t[p].size--;
    if(key==t[p].key
       ||(key<t[p].key&&!t[p].l)
       ||(key>t[p].key&&!t[p].r))
    {
        person tkey=t[p].key;
        if(t[p].l&&t[p].r)
        {
            person ttkey=key;
            ttkey.key++;
            ttkey.kth++;
            t[p].key=remove(t[p].l,ttkey);
        }
        else
            p=t[p].l+t[p].r;
        return tkey;
    }
    else if(key<t[p].key) return remove(t[p].l,key);
    else return remove(t[p].r,key);
}
int succ(int &p,int pp,int key)
{
    if(p==0) return pp;
    else if(t[p].key.key>=key) return succ(t[p].l,p,key);
    else return succ(t[p].r,pp,key);
}
int main()
{
    int cas,n,x;
    char op[7];
    person key;
    scanf("%d",&cas);
    for(int kcas=1;kcas<=cas;kcas++)
    {
        printf("Case #%d:\n",kcas);
        top=root=0;
        int cnt=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",op);
            if(op[0]=='A')
            {
                scanf("%s%d",key.name,&key.key);
                key.kth=++cnt;
                insert(root, key);
                printf("%d\n",t[root].size);
            }
            else
            {
                scanf("%d",&x);
                int id=succ(root,0,x);
                if(!id) printf("WAIT...\n");
                else
                {
                    printf("%s\n",t[id].key.name);
                    remove(root,t[id].key);
                }
            }
        }
    }
    return 0;
}



版权声明:本文为fallen_fall原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。