Splay——kuangnin的模版
来源:互联网 发布:悟空传中的网络元素 编辑:程序博客网 时间:2024/05/16 23:41
bzoj1588#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;const int MAXN=1000010;int pre[MAXN],ch[MAXN][2],key[MAXN];int root,tot1;void NewNode(int &r,int father,int k){ r=++tot1; pre[r]=father; ch[r][0]=ch[r][1]=0; key[r]=k;}void Init(){ root=tot1=0; ch[root][0]=ch[root][1]=key[root]=pre[root]=0;}//旋转void Rotate(int x,int kind){ int y=pre[x]; ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y; pre[y]=x;}//Splay调整void Splay(int r,int goal){ while(pre[r]!=goal) { if(pre[pre[r]]==goal) Rotate(r,ch[pre[r]][0]==r); else { int y=pre[r]; int kind=ch[pre[y]][0]==y; if(ch[y][kind]==r) { Rotate(r,!kind); Rotate(r,kind); } else { Rotate(y,kind); Rotate(r,kind); } } } if(goal==0)root=r;}void Insert(int k)//插入一个值为k的结点{ int r=root; if(r==0) { NewNode(root,0,k); return; } while(ch[r][key[r]<k]) { r=ch[r][key[r]<k]; } NewNode(ch[r][key[r]<k],r,k); Splay(ch[r][key[r]<k],0);}int Get_Min(int r){ while(ch[r][0]) { r=ch[r][0]; } return r;}int Get_Max(int r){ while(ch[r][1]) { r=ch[r][1]; } return r;}int main(){ int n,num,ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) { if(scanf("%d",&num)==EOF)num=0; Insert(num); if(i==1) ans+=num; else { int tmp=INF; if(ch[root][0]) tmp=min(tmp,key[root]-key[Get_Max(ch[root][0])]); if(ch[root][1]) tmp=min(tmp,key[Get_Min(ch[root][1])]-key[root]); ans+=tmp; } } printf("%d\n",ans); return 0;}
0 0
- Splay——kuangnin的模版
- splay的模版
- splay模版
- splay模版
- Splay 模版
- 自己的splay模版(bzoj1588)
- splay树模版
- Splay树模版
- 开学splay(这个模版要背的滚瓜烂熟)
- 模版——函数模版、类模版
- Uva11922——splay
- Splay——kuangbin
- HNOI 2000 Splay Tree 模版题目
- [BZOJ 1588]营业额统计 && splay部分模版
- 【Hihocder 1329】——Splay
- Splay tree的splay操作
- C++——模版:函数模版
- C++——模版:类模版
- 简单的用列子理解ajax同步异步
- 进程五种基本状态
- Codeforces gym 100685 E
- CSU-1804-有向无环图-拓扑排序+DP
- Java注解
- Splay——kuangnin的模版
- struts.xml配置文件的内容标签
- POJ - 1042 Gone Fishing 钓鱼 动态规划(DP)
- 360HBase二级索引方案学习总结
- Java基础知识
- Java异常总结
- System.out.println()和System.out.write()
- HDU:FATE
- 搜狐笔试编程题(2016研发岗)