Splay tree入门题,学好代码风格,学习HH大牛的,传送门。。

 #include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int pre[N],key[N],ch[N][],root,tot; //分别表示父结点,键值,左右孩子(0为左孩子,1为右孩子),根结点,结点数量
int n;
//新建一个结点
void addn(int &r,int fa,int k)
{
r=++tot;
pre[r]=fa;
key[r]=k;
ch[r][]=ch[r][]=; //左右孩子为空
}
//旋转,kind为1为右旋,kind为0为左旋
int Rotate(int x,int kind)
{
int y=pre[x],z=pre[y];
//类似SBT,要把其中一个分支先给父节点
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
//如果父节点不是根结点,则要和父节点的父节点连接起来
if(z)ch[z][ch[z][]==y]=x;
pre[x]=z;
ch[x][kind]=y;
pre[y]=x;
}
//Splay调整,将根为r的子树调整为goal
int Splay(int r,int goal)
{
int y,kind;
while(pre[r]!=goal){
//父节点即是目标位置,goal为0表示,父节点就是根结点
y=pre[r];
if(pre[y]==goal){
Rotate(r,ch[y][]==r);
}
else {
kind=ch[pre[y]][]==y;
//两个方向不同,则先左旋再右旋
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}
//两个方向相同,相同方向连续两次
else {
Rotate(y,kind);
Rotate(r,kind);
}
}
}
//更新根结点
if(goal==)root=r;
} int Insert(int k)
{
int r=root;
while(ch[r][k>key[r]]){
//不重复插入
if(key[r]==k){
Splay(r,);
return ;
}
r=ch[r][k>key[r]];
}
addn(ch[r][k>key[r]],r,k);
//将新插入的结点更新至根结点
Splay(ch[r][k>key[r]],);
return ;
}
//找前驱,即左子树的最右结点
int getpre(int x)
{
if(!ch[x][])return -INF;
x=ch[x][];
while(ch[x][])x=ch[x][];
return key[x];
}
//找后继,即右子树的最左结点
int getsuf(int x)
{
if(!ch[x][])return INF;
x=ch[x][];
while(ch[x][])x=ch[x][];
return key[x];
} int main()
{
// freopen("in.txt","r",stdin);
int i,a,ans;
while(~scanf("%d",&n))
{
ans=root=tot=;
for(i=;i<n;i++){
if(scanf("%d",&a)==EOF)a=;
if(i==){
ans+=a;
addn(root,,a);
}
else {
if(Insert(a)==)continue;
ans+=Min(a-getpre(root),getsuf(root)-a);
}
}
printf("%d\n",ans);
}
return ;
}

最新文章

  1. Linux入门之路
  2. Oracle在存储过程中如何返回结果集
  3. Oracle 分区表的新增、修改、删除、合并。普通表转分区表方法
  4. 纯window下VMware 安装 OS X El Capitan 原版映像【未完待续】
  5. 8 个必备的PHP功能开发
  6. zepto判断手机横竖屏
  7. 简单重置Centos服务器中Mysql的root密码
  8. 详解Linux高效命令head、tail和cat
  9. Windbg驱动双机调试环境配置
  10. CASE函数
  11. Tornado cookie 笔记
  12. linux-centos使用 wget命令获取jdk
  13. linux学习笔记-10.解压与压缩
  14. mysql replace into用法详细说明
  15. Selenium2+python自动化54-unittest生成测试报告(HTMLTestRunner)
  16. JPush删除别名及回调函数(SWIFT)
  17. sql server 查询ntext字段长度
  18. BZOJ2530 [Poi2011]Party 【贪心】
  19. 普通平衡树Tyvj1728、luogu P3369 (treap)
  20. code M资格赛 补题

热门文章

  1. 慢查询日志分析工具之pt-query-digest
  2. FreeRTOS Memory Management ( IAR )
  3. GNU 是什么?
  4. Spring通过Gmail SMTP服务器MailSender发送电子邮件
  5. (转)RTMP中FLV流到标准h264、aac的转换
  6. 我的一些简单的shell脚本实例
  7. CSS学习要点
  8. [置顶] JDK工具(零)--简要介绍JDK1.6自带的42个工具
  9. 出现&quot;未将对象引用设置到对象的实例“问题的总结
  10. 【帧动画总结】AnimationDrawable Frame