营业额统计
Posted liuzz-20180701
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了营业额统计相关的知识,希望对你有一定的参考价值。
题目描述
思路
代码
#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;
以上是关于营业额统计的主要内容,如果未能解决你的问题,请参考以下文章