[bzoj] 1588 营业额统计 || Splay板子题
原题
给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\)
Splay板子题。
Splay讲解:http://www.cnblogs.com/mrsheep/p/8110483.html
//太懒了……
#include<cstdio>
#include<algorithm>
#include<cmath>
#define which(x) (ls[f[(x)]]==(x))
#define N 33000
using namespace std;
int n,v,root,ls[N],rs[N],val[N],f[N],idx,ans;
void Rotate(int u)
{
int v=f[u],w=f[v],b=which(u)?rs[u]:ls[u],dir=which(v);
which(u)?(rs[u]=v,ls[v]=b):(ls[u]=v,rs[v]=b);
f[v]=u;f[b]=v;f[u]=w;
if (w) dir?ls[w]=u:rs[w]=u;
}
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void splay(int x,int y)
{
while (f[x]!=y)
{
if (f[f[x]]!=y)
{
if (which(x)==which(f[x])) Rotate(f[x]);
else Rotate(x);
}
Rotate(x);
}
if (!y) root=x;
}
void insert(int x)
{
int u=root,v=0;
while (u)
{
v=u;
if (x<val[u]) u=ls[u];
else u=rs[u];
}
f[++idx]=v;
val[idx]=x;
if (v) x<val[v]?ls[v]=idx:rs[v]=idx;
splay(idx,0);
}
int getmx(int x)
{
if (!x) return 97797977;
while (rs[x]) x=rs[x];
return val[x];
}
int getmn(int x)
{
if (!x) return 97797977;
while (ls[x]) x=ls[x];
return val[x];
}
int query()
{
int lmx=getmx(ls[root]),rmn=getmn(rs[root]);
return min(abs(val[root]-lmx),abs(val[root]-rmn));
}
int main()
{
n=read();
v=read();
insert(v);
ans+=v;
for (int i=2;i<=n;i++)
{
v=read();
insert(v);
ans+=query();
}
printf("%d",ans);
return 0;
}
[bzoj] 1588 营业额统计 || Splay板子题的更多相关文章
- BZOJ 1588 营业额统计 Splay
主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...
- BZOJ 1588 营业额统计
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- (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】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- 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 ...
随机推荐
- Ubuntu卡在logo界面
对于这个问题,我也是在最近一次偶然的机会中发现的. 我重装了了Ubuntu 18.04, 很多东西需要重新配置, 有个刚性需求就是配置shadowsocks实现***,对于从windows向linu ...
- [CodeForce721C]Journey
题目描述 Recently Irina arrived to one of the most famous cities of Berland - the Berlatov city. There a ...
- ORACLE高级部分内容
1.pl/sql基本语句 DECLARE BEGIN END; / 循环语句 DECLARE I NUMBER(2):=1; BEGIN WHILE I<100 LOOP I:=I+1; EN ...
- 基础的表ADT -数据结构(C语言实现)
读数据结构与算法分析 表的概述 形如A1,A2,A3... 操作合集 PrintList MakeEmpty Find Insert Delete 表的简单数组实现 分析: PrintList和Fin ...
- 日历(Calendar)模块
#usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...
- Deep Residual Learning for Image Recognition论文笔记
Abstract We present a residual learning framework to ease the training of networks that are substant ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- SpringBoot在IDEA下使用JPA
1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...
- js实现滑动器效果
最近公司在做一个项目,页面中要用到滑动器效果,我的第一反应是使用HTML5 input类型中的range类型,但马上我就否定了这个想法,因为range类型存在浏览器的兼容性问题(在主流浏览器中).但又 ...
- 20145214 《Java程序设计》第1周学习总结
20145214 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 了解了Java的诞生和版本演进的历史,目前的最新版本是Java SE8. java的三大平台分别是Java ...