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)的更多相关文章

  1. BZOJ 1588 营业额统计 Splay

    主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...

  2. [bzoj] 1588 营业额统计 || Splay板子题

    原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...

  3. BZOJ 1588 营业额统计

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

  4. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  5. BZOJ 1588 营业额统计 set

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1588 题目大意: 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交 ...

  6. bzoj 1588营业额统计(HNOI 2002)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 splay  bottom-up的数组实现. 题意就是给你一组数,求每个数与在其前面且与其最相 ...

  7. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  8. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

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

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

  10. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

随机推荐

  1. "淘宝推荐系统简介"分享总结

    概述: 此分享是关于淘宝推荐系统简介 1.推荐引擎就是:如何找到用户感兴趣的东西和以什么形式告诉用户:2.推荐引擎的作用:提高用户忠诚度,提高成交转化率和提高网站交叉销售能力:3.推荐系统核心:产品, ...

  2. linux e2fsprogs安装解决uuid/uuid.h: No such file or directory错误

    linux查看某个包是否安装    dpkg -l libuu*    用gcc编译发生nux 错误:fatal error: uuid/uuid.h: No such file or directo ...

  3. Swift游戏实战-跑酷熊猫 02 创建熊猫类

    要点: 如何继承SKSpriteNode :子类必须调用SKSpriteNode的一个指定构造器 init(){ super.init(texture:texture,color:UIColor.wh ...

  4. HttpContext.Current 的缺陷

    了解ASP.NET的开发人员都知道它有个非常强大的对象 HttpContext,而且为了方便,ASP.NET还为它提供了一个静态属性HttpContext.Current来访问它,今天的博客打算就从H ...

  5. (转载)R14也称作子程序连接寄存器

    R14也称作子程序连接寄存器(Subroutine Link Register)或连接寄存器LR.当执行BL子程序调用指令时,R14中得到R15(程序计数器PC)的备份. 其他情况下,R14用作通用寄 ...

  6. windows系统调用 线程 启动与挂起

    #include "iostream" #include "windows.h" using namespace std; class CWorkerThrea ...

  7. BaseAction的一般写法

    package com.mi.action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import ja ...

  8. Android小案例——简单图片浏览器

    今天上午休息看Android书,里面有个变化图片的示例引起了我的兴趣. 示例需求: 有N张图片,循环显示图片的内容.如果需求让我写我会使用一个变量count来保存显示图片数据的索引,图片显示时做个判断 ...

  9. linux中编译git时提示找不到ssl.h头文件

    在centos中的解决方案是安装一个叫 openssl-devel 的包.

  10. yii2的windows下安装及前期步骤

    Yii2的安装(以生成basic目录为例) 第一步:服务器安装好后生成www目录,在该目录下新建yii2目录,把下载的compser.phar包放在该目录下 第二步:dos命令下进入项目目录 第三步: ...