【Tyvj1185】【codevs1296】【BZOJ1588】营业额统计,Splay练习

传送门1
传送门2
传送门3
写在前面:身体不舒服,感觉有点差
思路:查询时先找是否有这个元素,没有的话再分别找前驱后继。
注意:BZOJ数据有错,详见讨论版
代码:

#include<bits/stdc++.h>
using namespace std;
int n,x,root,tot,p,q,ans,fuck;
struct os
{
    int fa,occ,sz,ch[2],data;
}a[100000];
void ct(int now)
{
    a[now].sz=a[now].occ+a[a[now].ch[0]].sz+a[a[now].ch[1]].sz;
}
void made(int x)
{
    a[++tot].data=x;
    a[tot].occ=a[tot].sz=1;
}
void rorate(int now,bool flag)
{
    int pa=a[now].fa;
    a[pa].ch[!flag]=a[now].ch[flag];
    a[a[now].ch[flag]].fa=pa;
    if (a[a[pa].fa].ch[0]==pa) a[a[pa].fa].ch[0]=now;
    else a[a[pa].fa].ch[1]=now;
    a[now].fa=a[pa].fa;
    a[pa].fa=now;
    a[now].ch[flag]=pa;
    ct(pa);
    ct(now);
}
void splay(int now,int goal)
{
    int pa;
    while (a[now].fa!=goal)
    {
        pa=a[now].fa;
        if (a[pa].fa==goal)
        {
            if (a[pa].ch[0]==now) rorate(now,1);
            else rorate(now,0);
        }
        else if (a[a[pa].fa].ch[0]==pa)
        {
            if (a[pa].ch[0]==now) rorate(pa,1);
            else rorate(now,0);
            rorate(now,1);
        }
        else
        {
            if (a[pa].ch[1]==now) rorate(pa,0);
            else rorate(now,1);
            rorate(now,0);
        }
    }
    if (!goal) root=now;
}
void insert(int x)
{
    if (!root) {made(x);root=tot;return;}
    int now=root;
    while (now)
    {
        a[now].sz++;
        if (x==a[now].data){a[now].occ++;splay(now,0);return;}
        else if (x<a[now].data)
        {
            if (!a[now].ch[0])
            {
                made(x);
                a[now].ch[0]=tot;
                a[tot].fa=now;
                splay(tot,0);
                return;
            }
            else now=a[now].ch[0];
        }
        else
        {
            if (!a[now].ch[1])
            {
                made(x);
                a[now].ch[1]=tot;
                a[tot].fa=now;
                splay(tot,0);
                return;
            }
            else now=a[now].ch[1];
        }
    }
}
int find(int x)
{
    int now=root;
    while (now)
    {
        if (a[now].data==x) return now;
        if (a[now].data>x) now=a[now].ch[0];
        else now=a[now].ch[1];
    }
    return 0;
}
int find_next_max(int x)
{
    int now=root,ans=0;
    while (now)
    {
        if (a[now].data>x)
        ans=(!ans||a[ans].data>a[now].data)?now:ans,
        now=a[now].ch[0];
        else now=a[now].ch[1];
    }
    if (ans) splay(ans,0);
    return ans;
}
int find_next_min(int x)
{
    int now=root,ans=0;
    while (now)
    {
        if (a[now].data<x)
        ans=(!ans||a[ans].data<a[now].data)?now:ans,
        now=a[now].ch[1];
        else now=a[now].ch[0];
    }
    if (ans) splay(ans,0);
    return ans;
}
main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        if (scanf("%d",&x)==EOF) x=0;
        if (find(x)) {insert(x);continue;}
        p=find_next_max(x);
        q=find_next_min(x);
        if (!p&&!q) ans+=x;
        else if (!p) ans+=x-a[q].data;
        else if (!q) ans+=a[p].data-x;
        else ans+=(a[p].data-x>x-a[q].data)?x-a[q].data:a[p].data-x;
        insert(x);
    }
    printf("%d",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值