bzoj 1588 [HNOI2002]营业额统计

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1588

Splay模板题。。


#include <cstdio>
#include <iostream>
using namespace std;
#define Key_value (ch[root][ch[root][1]])

const int inf = 0x3f3f3f3f;
const int maxn = 1e6+10;

// splay
int tot1, root;
int pre[maxn], ch[maxn][2];
int key[maxn];

void newNode(int &r, int father, int k)
{
    r = ++tot1;
    pre[r] = father;
    key[r] = k;
    ch[r][0] = ch[r][1] = 0;
}

void init()
{
    pre[0] = ch[0][0] = ch[0][1] = 0;
    tot1 = root = 0;
}

void Rotate(int x, int kind)
{
    int y = pre[x];

    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind]] = y;

    if(pre[y]) ch[pre[y]][y == ch[pre[y]][1]] = x;

    pre[x] = pre[y];
    pre[y] = x;
    ch[x][kind] = y;
}

void Splay(int r, int goal)
{
    while(pre[r] != goal)
    {
        if(pre[pre[r]] == goal) Rotate(r, r == ch[pre[r]][0]);
        else
        {
            int y = pre[r];
            int kind = (y == ch[pre[y]][0]);

            if(r == ch[y][kind])
            {
                Rotate(r, !kind);
                Rotate(r, kind);
            }
            else
            {
                Rotate(y, kind);
                Rotate(r, kind);
            }
        }
    }
    if(goal == 0) root = r;
}

void Insert(int k)
{
    int r = root;
    while( ch[r][key[r] < k] )
    {
        r = ch[r][key[r]<k];
    }
    newNode(ch[r][key[r]<k], r, k);
    Splay(ch[r][key[r]<k], 0);
}

// 前驱:左子树的最右端点
int get_pre(int x)
{
    int r = ch[x][0];
    if(r == 0) return inf;
    while(ch[r][1]) r = ch[r][1];
    return key[x] - key[r];
}

//后继:右子树的最左端点
int get_next(int x)
{
    int r = ch[x][1];
    if(r == 0) return inf;
    while(ch[r][0]) r = ch[r][0];
    return key[r] - key[x];
}

int main()
{
    int t, ans = 0, n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
    {
        if( scanf("%d", &t) == EOF) t = 0;  // 测试数据有问题。。
        Insert(t);
        if(i == 0)
        {
            ans = t;
            newNode(root, 0, t);
        }
        else
        {
            int a = get_pre(root);
            int b = get_next(root);
            ans += min(a, b);
        }
    }
    cout << ans << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值