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

营业额统计

时间:2019-09-18 17:52:28      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:for   amp   node   inline   line   struct   void   +=   const   

题目描述

思路

代码

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#define min(a, b) ((a) < (b) ? a : b)

const int MAX = (1<<15) + 5;
int n, m, tot, rt, inf = 0x3f3f3f3f;
long long ans;
struct Node {
    int lc, rc, cnt, size, key, pri;
    #define lc(x) t[x].lc
    #define rc(x) t[x].rc
    #define cnt(x) t[x].cnt
    #define size(x) t[x].size
    #define key(x) t[x].key
    #define pri(x) t[x].pri
} t[MAX];

void update(int &r) { size(r) = size(lc(r)) + size(rc(r)) + cnt(r); }

void zig(int &r) {
    int s = lc(r);
    lc(r) = rc(s);
    rc(s) = r;
    size(s) = size(r);
    update(r);
    r = s;
}

void zag(int &r) {
    int s = rc(r);
    rc(r) = lc(s);
    lc(s) = r;
    size(s) = size(r);
    update(r);
    r = s;
}

void insert(int &r, int k) {
    if (!r) {
        r = ++tot;
        cnt(r) = size(r) = 1;
        pri(r) = rand(), key(r) = k;
        return;
    } else ++size(r);
    if (k == key(r)) ++cnt(r);
    else if(k < key(r)) {
        insert(lc(r), k);
        if (pri(lc(r)) < pri(r)) zig(r);
    } else {
        insert(rc(r), k);
        if (pri(rc(r)) < pri(r)) zag(r);
    }
}

void delt(int &r, int k) {
    if (k == key(r)) {
        if (cnt(r) >= 2) cnt(r)--, size(r)--;
        else if (!lc(r) || !rc(r)) r = lc(r) + rc(r);
        else if (pri(lc(r)) < pri(r)) zag(r), delt(r, k);
        else zig(r), delt(r, k);
        return;
    }
    if (k < key(r)) delt(lc(r), k);
    else delt(rc(r), k);
}

int queryPre(int k) {
    int r = rt, res = inf;
    while (r) {
        if (key(r) <= k) res = key(r), r = rc(r);
        else r = lc(r);
    }
    return res;
}

int queryNxt(int k) {
    int r = rt, res = inf;
    while (r) {
        if (key(r) > k) res = key(r), r = lc(r);
        else r = rc(r);
    }
    return res;
}

int queryKth(int k) {
    int r = rt, res = inf;
    while (r) {
        if (size(lc(r)) < k && size(lc(r)) + cnt(r) >= k) return  key(r);
        else if (size(lc(r)) >= k) r = lc(r);
        else k -= size(lc(r)) + cnt(r), r = rc(r); 
    }
    return res;
}

int queryRand(int k) {
    int r = rt, res = 0;
    while (r) {
        if (k == key(r)) return size(lc(r)) + cnt(r) + 1;
        else if (k < key(r)) r = lc(r);
        else res += size(lc(r)) + cnt(r), r = rc(r);
    }
    return res;
}

inline int read() {
    int s = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * f;
}
void write(long long x) {
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
int main() {
    srand(time(NULL));
    n = read(), m = read();
    ans = m;
    insert(rt, m);
    for (int i = 2, j; i <= n; ++i) {
        j = read();
        int a = queryPre(j), b = queryNxt(j);
        // printf("pre:%d nxt:%d ", a, b);
        ans += min(abs(j - a), b - j);
        // printf("ans: %d\n", ans);
        insert(rt, j);
    }
    write(ans);
    return 0;
}

营业额统计

标签:for   amp   node   inline   line   struct   void   +=   const   

原文地址:https://www.cnblogs.com/liuzz-20180701/p/11543754.html

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