zoukankan      html  css  js  c++  java
  • [HNOI2002]营业额统计

    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;
    }
  • 相关阅读:
    通用爬虫和聚焦爬虫
    分布式缓存的介绍
    点击按钮执行后台方法
    jsp页面设置绝对路径
    vim调试
    图解Java 垃圾回收机制
    Java String 综述(上篇)
    Java 内部类综述
    深入理解Java类加载器(二):线程上下文类加载器
    深入理解Java类加载器(一):Java类加载原理解析
  • 原文地址:https://www.cnblogs.com/shandongs1/p/8028823.html
Copyright © 2011-2022 走看看