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基本的操作左旋,右旋: ...
随机推荐
- IOS开发基础知识--碎片42
1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...
- ObjectAnimator属性动画应用demo
感谢慕课网--eclipse_xu 布局文件:activity_main.xml <FrameLayout xmlns:android="http://schemas.android. ...
- iOS Xcode, 解决“Could not insert new outlet connection: Could not find any information for the class named”的问题。
在Xcode中,我们可以在StoryBoard编辑界面或者是xib编辑界面中通过“Control键+拖拽“的方式将某个界面元素和对应的代码文件连接起来,在代码文件中创建outlet. 不过,如果你的运 ...
- databtables 设置(显示)行号
var table = $('#priceStrategtyTable').DataTable({ "rowCallback": function( row, da ...
- active_record的不定时更新
1 valid?方法只是校验所有的校验条件,如果规则A仅加在数据库上,而不是model上,是不对校验起作用的,最后提交时虽然校验成功,但仍然会抛异常.比如数据库中增加了非空的规则,但模型上没有pres ...
- 常用API——Math对象型、Number型
Math.abs(num) : 返回num的绝对值 Math.acos(num) : 返回num的反余弦值 Math.asin(num) : 返回num的反正弦值 Math.atan(num) : 返 ...
- 原生JS 表单提交验证器
转载:http://www.cnblogs.com/sicd/p/4613628.html 一.前言 最近在开发一个新项目,需要做登陆等一系列的表单提交页面.在经过“缜密”的讨论后,我们决定 不用外部 ...
- jquery通过class验证表单不能为空
在开发系统时,往往都有某些表单数据为必填项,若用jQuery通过ID去验证,不仅会影响效率,还会有所遗漏,不易于后期维护. 本章将介绍如何利用jQuery,通过为表单配置class进行统一验证.(ID ...
- Linq专题之提高编码效率—— 第三篇 你需要知道的枚举类
众所周知,如果一个类可以被枚举,那么这个类必须要实现IEnumerable接口,而恰恰我们所有的linq都是一个继承自IEnumerable接口的匿名类, 那么问题就来了,IEnumerable使了 ...
- strcpy 函数的实现
原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开始且含有‘\0’结束符的字符串赋值到以d ...