Problem

每次给你一个数,找出前面的数与这个数的差的绝对值的最小值

Solution

Splay

Notice

找不到前驱和后继时,会出错。

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 40000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int pre, suf, point = 0, root;
struct node
{
int val[N + 5], count[N + 5], num[N + 5], son[2][N + 5], parent[N + 5];
inline void up(int u)
{
count[u] = count[son[0][u]] + count[son[1][u]] + num[u];
} void Rotate(int x, int &rt)
{
int y = parent[x], z = parent[y];
int l = (son[1][y] == x), r = 1 - l;
if (y == rt) rt = x;
else if (son[0][z] == y) son[0][z] = x;
else son[1][z] = x;
parent[x] = z;
parent[son[r][x]] = y, son[l][y] = son[r][x];
parent[y] = x, son[r][x] = y;
up(y);
up(x);
} void Splay(int x, int &rt)
{
while (x != rt)
{
int y = parent[x], z = parent[y];
if (y != rt)
{
if ((son[0][z] == y) ^ (son[0][y] == x))
Rotate(x, rt);
else Rotate(y, rt);
}
Rotate(x, rt);
}
} void Insert(int &u, int x, int last)
{
if (u == 0)
{
u = ++point;
val[u] = x, parent[u] = last, num[u] = count[u] = 1;
Splay(u, root);
}
else
{
if (x > val[u]) Insert(son[1][u], x, u);
else if (x < val[u]) Insert(son[0][u], x, u);
else if (x == val[u]) num[u]++, count[u]++, Splay(u, root);
}
} void Delete(int x)
{
Splay(x, root);
if (num[x] > 1)
{
num[x]--, count[x]--;
return;
}
if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
else
{
int t = son[1][x];
while (son[0][t] != 0) t = son[0][t];
Splay(t, root);
son[0][t] = son[0][x], parent[son[0][x]] = t;
up(t);
}
parent[root] = 0;
} void Find_pre(int u, int x)
{
if (u == 0) return;
if (x >= val[u])
{
pre = u;
Find_pre(son[1][u], x);
}
else Find_pre(son[0][u], x);
} void Find_suf(int u,int x)
{
if (u == 0) return;
if (x <= val[u])
{
suf = u;
Find_suf(son[0][u], x);
}
else Find_suf(son[1][u], x);
} int Find_num(int u, int x)
{
if (x <= count[son[0][u]]) return Find_num(son[0][u], x);
if (x > count[son[0][u]] + num[u]) return Find_num(son[1][u], x - count[son[0][u]] - num[u]);
return val[u];
} int Find_id(int u, int x)
{
if (x == val[u]) return u;
if (x > val[u]) return Find_id(son[1][u], x);
if (x < val[u]) return Find_id(son[0][u], x);
}
}Splay_tree;
int main()
{
int n = read(), x = read();
int ans = x;
Splay_tree.Insert(root, x, 0);
for (int i = 2; i <= n; i++)
{
x = read();
pre = 0, suf = n + 1;
Splay_tree.Find_pre(root, x);
Splay_tree.Find_suf(root, x);
if (pre == 0) ans += Splay_tree.val[suf] - x;
else if (suf == n + 1) ans += x - Splay_tree.val[pre];
else ans += min(x - Splay_tree.val[pre], Splay_tree.val[suf] - x);
Splay_tree.Insert(root, x, 0);
}
printf("%d\n", ans);
}

[BZOJ1588]营业额统计的更多相关文章

  1. p2234&bzoj1588 营业额统计

    传送门 题目 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...

  2. BZOJ1588 营业额统计 (Splay)

    营业额统计 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...

  3. [BZOJ1588]营业额统计(Splay)

    Description 题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身) 如:5 1 2 5 4 6 Ans=5+|1-5|+|2-1|+|5- ...

  4. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  5. 营业额统计(bzoj1588)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  6. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  7. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  8. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  9. 【链表】BZOJ1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 17555  Solved: 7179[Submit][Sta ...

随机推荐

  1. JAVA基础知识总结:十二

    一.String类 字符串是一种特殊的对象,一旦被初始化就不能被改变了 字符串常量存储于常量池中 二.StringBuffer类 是一个字符串缓冲区,相当于一个容器 特点 a.可以对字符串进行增加和删 ...

  2. ado.net常用操作

    目录 一.ADO.NET概要 二.ADO.NET的组成 三.Connection连接对象 3.1.连接字符串 3.1.1.SQL Server连接字符串 3.1.2.Access连接字符串 3.1.3 ...

  3. 20165303 魏煜第四次实验 Android开发

    实验内容 1实验要求: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: 参考http://www.cnblog ...

  4. pythoncook 文件和io

    1.文件不存在,则写入:文件存在则,报错 try: with open('file','x') as f: f.write() except FileExistsError: print('file ...

  5. Android中SharedPerforences的简单使用示例 --Android基础

    SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...

  6. LeetCode--007--整数反转(java)

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出 ...

  7. p1518 The Tamworth Two

    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...

  8. Windows定时开机并开启工作软件

    开启休眠功能 在搜索窗口中输入“cmd.exe”,在结果中看见了“cmd.exe”,右击选择“以管理员权限运行程序”打开“cmd.exe”命令窗口,输入命令“powercfg -h on”即可开启计算 ...

  9. 创建gitlab ssh 密钥

    SSH代表用于管理网络,操作系统和配置的Secure Shell或Secure Socket Shell,并且每次都不需要使用用户名和密码即可验证GitLab服务器. 您可以设置SSH密钥以提供计算机 ...

  10. 『Yaml』配置文件读写包

    YAML 在Python中的配置应用 YAML 是专门用来写配置文件的语言,和JSON相近,都是对字典做规范化文件输出的 一.简介 YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读 ...