码迷,mamicode.com
首页 > 其他好文 > 详细

[HNOI2002]营业额统计

时间:2017-12-12 22:02:31      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:clu   lin   root   else   bre   bsp   color   int   inf   

https://daniu.luogu.org/problemnew/show/P2234

#include <bits/stdc++.h>

const int N = 1e5 + 10;

#define gc getchar()
#define inf 0x3f3f3f3f

int fa[N], ch[N][2], cnt[N], siz[N], data[N];
int n, Ti, root, answer;

inline int read(){
    int x = 0, f = 1; char c = gc;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = gc;} 
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x * f;
}

int son(int x){
    return x == ch[fa[x]][1];
}

void rotate(int x){
    int y = fa[x], z = fa[y], b = son(x), c = son(y), a = ch[x][!b];
    if(z) ch[z][c] = x; else root = x; fa[x] = z;
    if(a) fa[a] = y; ch[y][b] = a;
    ch[x][!b] = y; fa[y] = x;
}

void splay(int x, int i){
    while(fa[x] != i){
        int y = fa[x], z = fa[y];
        if(z == i) rotate(x);
        else if(son(x) == son(y)) rotate(y), rotate(x);
        else rotate(x), rotate(x);
    }
}

void ins(int rt, int v){
    int y, x = rt;
    while(1){
        y = ch[x][data[x] < v];
        if(!y){
            y = ++ n;
            root = n;
            data[y] = v;
            fa[y] = x;
            ch[x][data[x] < v] = y;
            break;
        }
        x = y;
    }
    splay(y, 0);
}

int getpre(int rt){
    int p = ch[rt][0];
    while(ch[p][1]) p = ch[p][1];
    return p;
}

int getsuc(int rt){
    int p = ch[rt][1];
    while(ch[p][0]) p = ch[p][0];
    return p;
} 

int main()
{
    Ti = read();
    ins(root, inf);
    ins(root, - inf);
    for(int i = 1; i <= Ti; i ++){
        int x = read();
        if(i == 1){
            answer += x;
            ins(root, x);
        }
        else {
            ins(root, x);
            int pre = getpre(root);
            int suc = getsuc(root);
            answer += std :: min(abs(x - data[pre]), abs(x - data[suc]));
        }
    }
    std :: cout << answer;
    return 0;
}

 

[HNOI2002]营业额统计

标签:clu   lin   root   else   bre   bsp   color   int   inf   

原文地址:http://www.cnblogs.com/shandongs1/p/8028823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!