BZOJ 1588 Splay伸展树

题目链接

题意:

n个整数,第一个整数支付第一个整数大小的费用,其他数字支付与已有数字(比如:相对第i个数字的已有数字为前i-1个数字)中差值最小的费用(比如:已有数字1 5 10),当输入数字8时需要支付2的费用10-8=2

思路:

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;
typedef double DB;
typedef long long LL;
typedef complex<double>CD;
const int maxn = 100010;
const int mod = 1e9+7;
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 ls[maxn],rs[maxn],fa[maxn],val[maxn],ans,root,sizes;

void R_rotate( int x )
{
    int y = fa[x];
    int z = fa[y];
    ls[y] = rs[x];
    if ( rs[x] )
        fa[rs[x]] = y;
    fa[x] = z;
    if ( z )
    {
        if ( ls[z]==y )
            ls[z] = x;
        else
            rs[z] = x;
    }
    rs[x] = y;
    fa[y] = x;
}

void L_rotate( int x )
{
    int y = fa[x];
    int z = fa[y];
    rs[y] = ls[x];
    if ( ls[x] )
        fa[ls[x]] = y;
    fa[x] = z;
    if ( z )
    {
        if ( ls[z]==y )
            ls[z] = x;
        else
            rs[z] = x;
    }
    ls[x] = y;
    fa[y] = x;
}

void Splay( int x , int ace )
{
    while ( fa[x]!=ace )
    {
        int y = fa[x];
        int z = fa[y];
        if ( z==ace )
        {
            if ( ls[y]==x )
                R_rotate( x );
            else
                L_rotate( x );
        }
        else
        {
            if ( ls[z]==y&&rs[y]==x )
                L_rotate( x ),R_rotate( x );
            else if ( rs[z]==y&&ls[y]==x )
                R_rotate( x ),L_rotate( x );
            else if ( ls[z]==y&&ls[y]==x )
                R_rotate( y ),R_rotate( x );
            else if ( rs[z]==y&&rs[y]==x )
                L_rotate( y ),L_rotate( x );
        }
    }
    if ( ace==0 )
        root = x;
}

int Pre( int x )
{
    int y = ls[x];
    if ( !y )
        return 0;
    while ( rs[y] )
        y = rs[y];
    return y;
}

int Suc( int x )
{
    int y = rs[x];
    if ( !y )
        return 0;
    while ( ls[y] )
        y = ls[y];
    return y;
}

void Insert( int k , int x )
{
    if ( x==val[k] )
        return;
    if ( x<val[k] )
    {
        if ( ls[k] )
            Insert( ls[k] , x );
        else
        {
            ++sizes,ls[sizes] = rs[sizes] = 0,fa[sizes] = k,ls[k] = sizes,val[sizes] = x;
            Splay ( sizes , 0 );
            int p = Pre( sizes );
            int q = Suc( sizes );
            int minx = INF;
            if ( p )
                minx = Min( minx , Abs( x-val[p] ) );
            if ( q )
                minx = Min( minx , Abs( x-val[q] ) );
            ans += minx;
        }
    }
    else
    {
        if ( rs[k] )
            Insert( rs[k] , x );
        else
        {
            ++sizes,ls[sizes] = rs[sizes] = 0,fa[sizes] = k,rs[k] = sizes,val[sizes] = x;
            Splay ( sizes , 0 );
            int p = Pre( sizes );
            int q = Suc( sizes );
            int minx = INF;
            if ( p )
                minx = Min( minx , Abs( x-val[p] ) );
            if ( q )
                minx = Min( minx , Abs( x-val[q] ) );
            ans += minx;
        }
    }
}

int main()
{
    for ( int n ; scanf( "%d" , &n )==1 ; )
    {
        scanf ( "%d" , &ans );
        root = sizes = 1;
        ls[1] = 0;
        rs[1] = 0;
        fa[1] = 0;
        val[1] = ans;
        for ( int i=1,x ; i<n ; i++ )
        {
            scanf ( "%d" , &x );
            Insert ( root , x );
        }
        printf ( "%d\n" , ans );
    }
    return 0;
}

猜你喜欢

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