P2234 [HNOI2002]营业额统计 Treap

洛谷题目:https://www.luogu.org/problem/show?pid=2234
set解法:http://blog.csdn.net/sdfzsyh/article/details/72790622

没错还是P2234
所以说我有多么爱这道题~~
当然这道很模板的题非常好~~
所以这道题对于自平衡搜索树初学者还是非常好的!

说白了,就是一道求前驱后继的题;
但是有一些点需要注意:
1、开始的时候插入极大值和极小值作为边界,否则会WA;
2、插入及旋转的取地址符千万不要忘。

还是贴代码吧=.=:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

const int inf=0x3f3f3f3f;
int a,n,sub,pro,tot,root,answer;

struct Treap
{
    int val;
    int pro;
    int num;
    int lch,rch;
    int ln,rn;
} treap[32800];

inline void turn_right(int &p)
{
    int t=treap[p].lch;
    treap[p].lch=treap[t].rch;
    treap[t].rch=p;
    p=t;
} 

inline void turn_left(int &p)
{
    int t=treap[p].rch;
    treap[p].rch=treap[t].lch;
    treap[t].lch=p;
    p=t;
}

void treap_insert(int &p,int x)
{
    if(p==0)
    {
        tot++;p=tot;
        treap[p].pro=rand();
        treap[p].val=x;
        return ;
    }
    if(treap[p].val<x)
    {
        treap_insert(treap[p].rch,x);
        if(treap[p].pro<treap[treap[p].rch].pro)turn_left(p);
    }
    else
    {
        treap_insert(treap[p].lch,x);
        if(treap[p].pro<treap[treap[p].lch].pro)turn_right(p);
    }
}

inline void qsub(int k,int x) 
{  
    if (!k) return;
    if (treap[k].val<=x)
    {
        sub=treap[k].val;
        qsub(treap[k].rch,x);
    }
    else qsub(treap[k].lch,x);
}

inline void qpro(int k,int x)  
{  
    if (!k) return;
    if (treap[k].val>=x)
    {
        pro=treap[k].val;
        qpro(treap[k].lch,x);
    }
    else qpro(treap[k].rch,x);
}

int main()
{
    root=0;
    scanf("%d",&n);
    scanf("%d",&a);
    treap_insert(root,inf);
    treap_insert(root,-inf); 
    treap_insert(root,a);
    answer=a;
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&a);
        qpro(root,a);
        qsub(root,a);
        answer+=min(abs(a-sub),abs(a-pro));
        treap_insert(root,a);
    }
    printf("%d",answer); 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值