BZOJ 1208 Splay伸展树

题目链接

题意:

一家宠物店可能会收到流浪宠物或者有顾客来领养一只宠物,每次收养会产生一种权值即顾客和宠物的权值差,求最终权值大小

根据当前收集数据为宠物还是顾客一下情况:

1.当输入一只宠物时,如果现在有想领养宠物的顾客等待,那么就选一个权值和宠物相差最小的顾客带走这只宠物,如果有相同的情况,让权值小的那个顾客带走宠物

2.当输入一名顾客时,如果现在有流浪宠物待在宠物店中,那么就选一个权值和顾客相差最小的宠物被顾客带走,如果有相同的情况,让权值小的那只宠物被顾客带走

思路:

Splay基础操作:插入,删除,Splay,求前驱,求后缀,此外还有一个小操作维护当前树的种类是顾客树还是宠物树

C++代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
typedef double DB;
typedef long long LL;
typedef complex<double>CD;
const int maxn = 100010;
const int mod = 1000000;
const int inf = 0x3f3f3f3f;
const int INF = 0x7FFFFFFF;

void Mod( int &x ){ x%=mod; }
int Min( int a , int b ){ return a<b?a:b; }
int Max( int a , int b ){ return a>b?a:b; }
int Abs( int x ){ return x>=0?x:-x; }
int Random(){ static int seed = 703; return seed=(int)(seed*48271LL%2147483647); }

int fa[maxn],son[maxn][2],val[maxn],root,sizes;

void Newnode( int data , int father )
{
    sizes++;
    fa[sizes] = father;
    son[sizes][0] = 0;
    son[sizes][1] = 0;
    val[sizes] = data;
}

void Rotate( int x , int kind )
{
    int y = fa[x];
    int z = fa[y];
    son[y][1-kind] = son[x][kind];
    if ( son[x][kind]!=0 )
        fa[son[x][kind]] = y;
    fa[x] = z;
    if ( z )
        son[z][son[z][1]==y] = x;
    fa[y] = x,son[x][kind] = y;
}

void Splay( int x )
{
    while ( fa[x]!=0 )
    {
        int y = fa[x];
        int z = fa[y];
        if ( z==0 )
            Rotate( x , x!=son[y][1] );
        else
        {
            if ( y==son[z][0] )
            {
                if ( x==son[y][0] )
                    Rotate( y , 1 ),Rotate( x , 1 );
                else
                    Rotate( x , 0 ),Rotate( x , 1 );
            }
            else
            {
                if ( x==son[y][0] )
                    Rotate( x , 1 ),Rotate( x , 0 );
                else
                    Rotate( y , 0 ),Rotate( x , 0 );
            }
        }
    }
    root = x;
}

void Insert( int data , int now )
{
    if ( root==0 )
        Newnode( data , 0 ),root = sizes;
    else
    {
        while ( true )
        {
            if ( data<val[now] )
            {
                if ( son[now][0] )
                    now = son[now][0];
                else
                {
                    Newnode( data , now );
                    son[now][0] = sizes;
                    return;
                }
            }
            else
            {
                if ( son[now][1] )
                    now = son[now][1];
                else
                {
                    Newnode( data , now );
                    son[now][1] = sizes;
                    return;
                }
            }
        }
    }
}

void Delete( int x )
{
    Splay( x );
    if ( son[x][0]==0&&son[x][1]==0 )
        root = 0;
    else if ( son[x][0]==0 )
        root = son[x][1],fa[son[x][1]] = 0;
    else if ( son[x][1]==0 )
        root = son[x][0],fa[son[x][0]] = 0;
    else
    {
        int y = son[x][0];
        while ( son[y][1] )
            y = son[y][1];
        fa[son[x][0]] = 0;
        Splay( y );
        son[y][1] = son[x][1];
        fa[son[x][1]] = y;
        root = y;
    }
}

int prenum,pre,sucnum,suc;

void Find_Pre( int data )
{
    int now = root;
    pre = 0,prenum = INF;
    while ( true )
    {
        if ( now==0 )
            return;
        if ( data>val[now]&&(data-val[now])<prenum )
            pre = now,prenum = data-val[now];
        if ( data<val[now] )
            now = son[now][0];
        else
            now = son[now][1];
    }
}

void Find_Suc( int data )
{
    int now = root;
    suc = -1,sucnum = INF;
    while ( true )
    {
        if( now==0 )
            return;
        if ( data<val[now]&&(val[now]-data)<sucnum )
            suc = now,sucnum = val[now]-data;
        if ( data<val[now] )
            now = son[now][0];
        else
            now = son[now][1];
    }
}

int main()
{
    for ( int n ; scanf ( "%d" , &n )==1 ; )
    {
        int kind,k,data,ans = 0;
        scanf ( "%d%d" , &kind , &data );
        sizes = 0;
        Newnode( data , 0 );
        root = sizes;
        for ( int i=2 ; i<=n ; i++ )
        {
            scanf ( "%d%d" , &k , &data );
            if ( root==0 )
            {
                kind = k;
                Newnode( data , 0 );
                root = sizes;
            }
            else
            {
                if ( k==kind )
                    Insert( data , root ),Splay( sizes );
                else
                {
                    Find_Pre( data );
                    Find_Suc( data );
                    if ( prenum<=sucnum )
                        Mod ( ans+=prenum ),Delete( pre );
                    else
                        Mod ( ans+=sucnum ),Delete( suc );
                }
            }
        }
        printf ( "%d\n" , ans );
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Game_Acm/article/details/81460187