bzoj1588
splay
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct data
{
int fa,l,r,size,cnt,key;
}tree[];
bool flag1,flag2;
int root,n,tot,ans,a,b;
int abs(int x)
{
return x>?x:-x;
}
void update(int x)
{
tree[x].size=tree[tree[x].l].size+tree[tree[x].r].size+tree[x].cnt;
}
void zig(int x)
{
int y=tree[x].fa;
int z=tree[x].r;
tree[y].l=z;
tree[z].fa=y;
tree[x].fa=tree[y].fa;
if(y==tree[tree[y].fa].l) tree[tree[y].fa].l=x;
else tree[tree[y].fa].r=x;
tree[x].r=y;
tree[y].fa=x;
update(x);
update(y);
}
void zag(int x)
{
int y=tree[x].fa;
int z=tree[x].l;
tree[y].r=z;
tree[z].fa=y;
tree[x].fa=tree[y].fa;
if(y==tree[tree[y].fa].l) tree[tree[y].fa].l=x;
else tree[tree[y].fa].r=x;
tree[x].l=y;
tree[y].fa=x;
update(x);
update(y);
}
void splay(int x)
{
if(!root)
{
root=x;
return;
}
while(tree[x].fa)
{
int y=tree[x].fa;
int z=tree[y].fa;
if(y==root)
{
if(x==tree[root].l) zig(x); else zag(x);
update(x);
break;
}
else if(y==tree[z].l&&x==tree[y].l) {zig(y); zig(x);}
else if(y==tree[z].r&&x==tree[y].r) {zag(y); zag(x);}
else if(y==tree[z].l&&x==tree[y].r) {zag(x); zig(x);}
else if(y==tree[z].r&&x==tree[y].l) {zig(x); zag(x);}
update(x);
}
root=x;
}
void insert(int x,int k)
{
if(tree[x].key<k)
{
if(tree[x].r==)
{
++tot;
tree[tot].fa=x;
if(x) tree[x].r=tot;
tree[tot].key=k;
tree[tot].cnt=;
update(tot);
update(x);
splay(tot);
return;
}
insert(tree[x].r,k);
update(x);
}
else
{
if(tree[x].l==)
{
++tot;
tree[tot].fa=x;
if(x) tree[x].l=tot;
tree[tot].key=k;
tree[tot].cnt=;
update(tot);
update(x);
splay(tot);
return;
}
insert(tree[x].l,k);
update(x);
}
}
void findnxt(int x,int k,int pd)
{
if(x==) return;
if(pd==)
{
if(tree[x].key<=k)
{
flag1=true;
a=tree[x].key;
findnxt(tree[x].r,k,pd);
} else findnxt(tree[x].l,k,pd);
}
else
{
if(tree[x].key>=k)
{
flag2=true;
b=tree[x].key;
findnxt(tree[x].l,k,pd);
} else findnxt(tree[x].r,k,pd);
}
}
int main()
{
scanf("%d",&n);
while(n--)
{
int x; scanf("%d",&x);
findnxt(root,x,);
findnxt(root,x,);
int temp=;
bool flag=false;
// printf("%d %d\n",a,b);
if(flag1)
{
temp=abs(a-x);
flag=true;
}
if(flag2)
{
if(flag) temp=min(temp,abs(b-x));
else temp=abs(b-x);
}
if(tree[root].size==) ans+=x;
else ans+=temp;
insert(root,x);
flag1=false;
flag2=false;
a=;
b=;
}
printf("%d",ans);
return ;
}
bzoj1588的更多相关文章
- 【bzoj1588】 HNOI2002—营业额统计
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 (题目链接) 题意 给出一个序列,对于每一个数,找出之前与它相差最小的数,两者相减取绝对值加入 ...
- BZOJ1588 [HNOI2002]营业额统计 set
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1588 题意概括 给出数列,求 ∑F[i],其中F[1] = a[1] , F[i] = min( ...
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- BZOJ1588[HNOI2002]营业额统计
传送门 平衡树常规题,给出两种实现算法 Treap版: //OJ 1610 //by Cydiater //2016.9.1 #include <iostream> #include &l ...
- BZOJ1588——[HNOI2002]营业额统计
1.题目大意:一个简单的treap模板题(我才不告诉你题目少一句话呢,看discuss去 2.分析:treap模板题 #include <cstdio> #include <cstd ...
- 营业额统计(bzoj1588)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- bzoj1588,1208,1503
进入splay tree的学习中: 据说splay tree在理论上功能十分强大,好好学: splay首先一定是一棵BST,所以记不得的时候画个图就明白: 首先总结一下splay基本的操作左旋,右旋: ...
随机推荐
- 在TableView上添加悬浮按钮
如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...
- Android工程师常见面试题集
本文汇总了朋友同事在面试过程中被经常问道的一些问题,讲解不详细,有需要特别了解的可以留言告诉我.持续更新中…… 1.接口回调机制 ①定义一个接口,定义接口中的方法: ②在数据产生的地方持有接口,并提供 ...
- 【代码笔记】iOS-圆角矩形
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- CSS3 text-shadow
<!DOCTYPE html > <html > <head> <meta charset="utf-8"> <title&g ...
- Cannot execute as the database principal because the principal "guest" does not exist, this type of principal cannot be impersonated, or you do not have permission.
今天遇到这样一个问题:一个系统的作业需要给系统Support人员开放SQL Agent下作业的查看.执行权限.数据库版本为SQL Server 2014 SP2,给这个系统Support人员的NT账号 ...
- Java实现不同的WebService 调用方式
请求过程分析: 1.使用get方式获取wsdl文件,称为握手 2.使用post发出请求 3.服务器响应成功 几种监听工具: http watch Web Service explorer eclips ...
- HDFS开发实例
1.列出HDFS中的文件 package com.hdfs.test; import java.io.BufferedReader; import java.io.IOException; impor ...
- C++基础——模拟事务 (2)Composite模式
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- Mysql有用的面试题
A.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...
- js 输出二维数组的最大值
function num(arr){ max=a[0][0]; for (var i = 0; i < a.length; i++) { for (var j = 0; j< a[i].l ...