[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 ...
随机推荐
- leetcode--笔记8 Fizz Buzz
题目要求: Write a program that outputs the string representation of numbers from 1 to n. But for multipl ...
- hadoop 家族图
hadoop家族
- JavaWeb——JavaWeb开发入门
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- Selenium 入门到精通系列:六
Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...
- Elasticsearch 相同内容文档,不同score(评分)的奇怪问题
原文:http://stackoverflow.com/questions/14580752/elasticsearch-gives-different-scores-for-same-documen ...
- CryptoZombies学习笔记——Lesson5
chapter1:token代币 简而言之,通证就是支持交易的包含一系列规范的函数接口的一个智能合约,发币可以用ERC20标准,但是像僵尸这种非同质化代币,需要用ERC721标准 chapter2:e ...
- hadoop参数(未完).md
我X,有违禁词.麻烦提醒一下哪个词好吗?
- POJ 2104 K-th Number(划分树)
Description You are working for Macrohard company in data structures department. After failing your ...
- Catch That Cow(BFS广搜)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- Thunder团队第六周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 邹双黛同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblog ...