【codevs 1296】营业额统计 水~~

今天下午先写一个Splay水题来复习一下Splay模板。是不是有点太水了做这种水题我有点良心不安。

可笑的是一开始我竟然WA了一组,看来是我低估水题的数据范围了,我是空节点直接返回inf或-inf,明显是不合理的。比赛时再犯这种低级错误就真的滚粗了。

2016-07-29 当时我确实狂妄自大~~~~(>_<)~~~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=1E9;
inline const int max(const int &a,const int &b){return a>b?a:b;}
inline const int min(const int &a,const int &b){return a<b?a:b;}
struct node{
    node();
    node *fa,*ch[2];
    int d;
    short pl(){return this==fa->ch[1];}
}*null;
node::node(){ch[0]=ch[1]=fa=null;}
namespace Splay{
    node *ROOT;
    void build(){
        null=new node;
        *null=node();
        ROOT=null;
    }
    void rotate(node *k){
        node *r=k->fa; if (k==null||r==null) return;
        int x=k->pl()^1;
        r->ch[x^1]=k->ch[x];
        r->ch[x^1]->fa=r;
        if (r->fa==null) ROOT=k;
        else r->fa->ch[r->pl()]=k;
        k->fa=r->fa; r->fa=k;
        k->ch[x]=r;
    }
    void splay(node *r,node *tar=null){
        for (;r->fa!=tar;rotate(r))
         if (r->fa->fa!=tar)rotate(r->pl()==r->fa->pl()?r->fa:r);
    }
    void insert(int x){
        if (ROOT==null){
            ROOT=new node;
            ROOT->d=x;
            return;
        }node *r=ROOT;
        while (1){
            int c;
            if (x<r->d) c=0;
            else c=1;
            if (r->ch[c]==null){
                r->ch[c]=new node;
                r->ch[c]->d=x;
                r->ch[c]->fa=r;
                splay(r->ch[c]);
                return;
            }else r=r->ch[c];
        }
    }
    int leftmax(){
        node *r=ROOT->ch[0];
        while (r->ch[1]!=null) r=r->ch[1];
        return r->d;
    }
    int rightmin(){
        node *r=ROOT->ch[1];
        while (r->ch[0]!=null) r=r->ch[0];
        return r->d;
    }
    int que(){
        int x=inf;
        if (ROOT->ch[0]!=null) x=ROOT->d-leftmax();
        if (ROOT->ch[1]!=null) x=min(x,rightmin()-ROOT->d);
        return x;
    }
}
int getint(){char c;int fh=1;while (!isdigit(c=getchar()))if(c=='-')fh=-1;int a=c-'0';while (isdigit(c=getchar()))a=a*10+c-'0';return a*fh;}
int main(){
    int n=getint();
    Splay::build();
    int x=getint();
    long long ans=x;
    Splay::insert(x);
    for (int i=1;i<n;++i){
        x=getint();
        Splay::insert(x);
        ans+=Splay::que();
    }printf("%lld\n",ans);
    return 0;
}

这样就可以了

posted @   abclzr  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
阅读排行:
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率
· AI Agent爆火后,MCP协议为什么如此重要!
点击右上角即可分享
微信分享提示