BZOJ 1588:营业额统计(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1588
题意:中文题意。
思路:每一个点每一个点插入Splay,然后插入新的一个点之后,查这个节点的前驱和后继,即左子树最右边的点和右子树最左边的点。然后再从两个点中取个差值较小的就是答案了。要注意Rotate的时候一些细节(要给 rt 的父亲的父亲更新其孩子的值),还有Splay的细节:如果 rt 和 父节点都是要旋转相同方向,应该先旋转父亲节点再旋 rt,如果旋转不同方向就都是旋 rt。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x7fffffff
#define N 40000
struct node
{
int val, fa, son[]; // val是节点权值,fa是父亲,son[0]是左儿子, son[1]是右儿子
}tree[N];
int cnt; // 节点数 void new_node(int f, int w, int kind)
{
cnt++;
memset(tree[cnt].son, , sizeof(tree[cnt].son));
tree[cnt].val = w;
tree[cnt].fa = f;
tree[f].son[kind] = cnt;
} void Rotate(int rt, int kind) // 旋转操作要注意更新 rt 的父亲的父亲的儿子的值
{
int y = tree[rt].fa;
tree[y].son[kind^] = tree[rt].son[kind];
if(tree[rt].son[kind] != ) tree[tree[rt].son[kind]].fa = y;
tree[rt].fa = tree[y].fa;
int z = tree[rt].fa;
if(tree[z].son[] == y) tree[z].son[] = rt; // 就是这里
else tree[z].son[] = rt;
tree[rt].son[kind] = y;
tree[y].fa = rt;
} void Splay(int rt, int goal)
{
if(tree[rt].fa == goal) return ;
while(tree[rt].fa != goal) {
int y = tree[rt].fa;
int z = tree[y].fa;
int kind1 = rt == tree[y].son[] ? : ;
int kind2 = y == tree[z].son[] ? : ;
if(z == goal) {
Rotate(rt, kind1);
} else {
if(kind1 == kind2) { // 连续左旋或者右旋
Rotate(y, kind2);
} else {
Rotate(rt, kind1); // 先左后右或者先右后左
}
Rotate(rt, kind2);
}
}
} void Insert(int rt, int fa, int val, int kind)
{
if(rt == ) {
new_node(fa, val, kind);
return ;
}
if(tree[rt].val >= val) Insert(tree[rt].son[], rt, val, );
else Insert(tree[rt].son[], rt, val, );
} int Find(int rt, int kind)
{
if(tree[rt].son[kind] == ) return rt; // 查先驱和后继节点
Find(tree[rt].son[kind], kind);
} int main()
{
int n;
while(~scanf("%d", &n)) {
long long ans = ;
cnt = ;
for(int i = ; i <= n; i++) {
int x;
scanf("%d", &x);
if(i == ) {
new_node(, x, );
ans += x;
} else {
Insert(cnt, , x, );
Splay(cnt, );
int pre = , suf = ;
pre = Find(tree[cnt].son[], );
suf = Find(tree[cnt].son[], );
int prev = INF, sufv = INF;
if(pre != ) prev = tree[pre].val;
if(suf != ) sufv = tree[suf].val;
ans += min(abs(x - prev), abs(x - sufv));
}
}
printf("%lld\n", ans);
}
return ;
}
BZOJ 1588:营业额统计(Splay)的更多相关文章
- BZOJ 1588 营业额统计 Splay
主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...
- [bzoj] 1588 营业额统计 || Splay板子题
原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...
- BZOJ 1588 营业额统计
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- (HYSBZ)BZOJ 1588 营业额统计
营业额统计 Time Limit: 5000MS Memory Limit: 165888KB 64bit IO Format: %lld & %llu Description 营业额 ...
- BZOJ 1588 营业额统计 set
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1588 题目大意: 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交 ...
- bzoj 1588营业额统计(HNOI 2002)
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 splay bottom-up的数组实现. 题意就是给你一组数,求每个数与在其前面且与其最相 ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
随机推荐
- CentOS下netstat + awk 查看tcp的网络连接状态
执行以下命令: #netstat -n | awk ‘/^tcp/ {++state[$NF]} END {for(key in state) print key."\t".sta ...
- 使用Fiddler对android应用抓包
工作原理 先上个图 此图一目了然,可以看出fiddler在请求中所处的位置,我们就可以确定它能干些什么. 它实际工作在本机的8888端口http代理,我们启动fiddler时,它会自动更改代理设置: ...
- thttpd增加gzip压缩响应报文体功能,以减少传输数据量
thttpd thttpd是一个非常小巧的轻量级web server,它非常非常简单,仅仅提供了HTTP/1.1和简单的CGI支持,在其官方网站上有一个与其他web server(如Apache, Z ...
- oracle pl sql 解锁表
select p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_name from v$pr ...
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
控制台程序. 在模型中表示数据视图的类用来显示草图并处理用户的交互操作,所以这种类把显示方法和草图控制器合并在一起.不专用于某个视图的通用GUI创建和操作在SketcherFrame类中处理. 模型对 ...
- 2.Spring Web MVC的优势
清晰的角色划分:前端控制器(DispatcherServlet).请求到处理器映射(HandlerMapping).处理器适配器(HandlerAdapter).视图解析器(ViewResolver) ...
- ImportError: No module named setuptools
Python第三方模块中一般会自带setup.py文件,在Windows环境下,我们只需要使用命令 cd c:\Temp\foo python setup.py install 两个命令就可以完成第三 ...
- Android图像处理之Bitmap类(zz)
Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bitmap来实现这些 ...
- 20145207 《Java程序设计》第二周学习总结
开源中国的代码托管 不算调查问卷的话,这是第二篇博客,怎么说呢……感觉好麻烦!哈哈哈哈!不过也就这样吧.按照同学传达的老师的意思就是“写博客就是在重复一天的所学,虽然可能会花一定的时间,但是对于自己是 ...
- bzoj4448 [Scoi2015]情报传递
第一问不解释,对于第二问的处理,可以使用cdq分治,假设分治的询问区间是[L,R],那么我们对于标号在[L,mid]的修改操作赋予一个权值,因为在当前[L,R]中[L,mid]的修改操作只会对[mid ...