splay-tree 是一个二叉排序树?

http://www.notonlysuccess.com/index.php/splay-tree/    羡慕(有ACM题目推荐)

http://www.cppblog.com/MiYu/archive/2010/11/12/133405.html   splay - tree    哭(好长)

http://www.cnblogs.com/UnGeek/archive/2013/04/10/3013333.html    比较白话,就是字太小,还稍显啰嗦。。。o(╯□╰)o  (includes demo)

http://www.link.cs.cmu.edu/cgi-bin/splay/splay-cgi.pl   splay-tree  demo  生气(很有趣的实现)

http://hi.baidu.com/zab08/item/6125607df52c0d356dc37c56  左旋右旋   得意

http://read.pudn.com/downloads99/ebook/404161/Splay.pdf  菜鸟笔记 人家无聊开始写的splaytree  可怜

http://www.cs.cmu.edu/~sleator/papers/self-adjusting.pdf  发明论文委屈(看不懂)

http://www.cnblogs.com/kuangbin/archive/2013/03/17/2964546.html  跟着kuangbin写splaytree 奋斗


注释不一定正确。。。o(╯□╰)o,可能错误的理解也写上去了。。。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <queue>
using namespace std;
#define CLR(c,v) memset(c,v,sizeof(c));
typedef pair<int,int> pii;

const int N   = 1e5 + 10;
const int INF = (0xff>>4);
struct splaytree{
	int pre[N];		// 父节点
	int key[N];		// 权值
	int ch[N][2];	// 左右孩子
	int root;		// 根
	int tot;		// 节点计数
	splaytree(){ tot=root=0; }
	void NewNode(int &r,int father,int x); // 树根取引用+父亲(默认0?)+新节点权
	void Update(int node,int father);
	int Insert(int value);
	void Rotate(int x,int type);
	void zig(int x);
	void zag(int x);
	void Splay(int r,int goal);
	int GetBigger(int node);
	int GetLess(int node);
	static const int left  = 0;
	static const int right = 1;
};
typedef splaytree ST;

void ST::NewNode(int &r,int father,int x){
	// 建立新节点
	r = ++tot;			// 当前这个点的标号就是r
	pre[r] = father;	// 当前点的父节点(默认0?)
	key[r] = x;         // 当前点权
	ch[r][left]  = 0;		// 左孩子
	ch[r][right] = 0;		// 右孩子
}
void ST::Rotate(int x,int type){
	// 旋转子树,type: 0左旋(zag),1右旋(zig)
	// 右旋的条件: 所旋转的结点x是其父亲结点y的左儿子, 左旋同理

	//		3                     1
	//	   / \		zig=>		 / \
	//	  1   4					0   3
	//	 / \		<=zag		   / \
	//	0   2					  2   4
	
	int y = pre[x];	// 保存父节点
	ch[y][!type] = ch[x][type];
	if(pre[y] != 0) // 当y不是root,x往上的时候要更新上面父节点的孩子
		ch[ pre[y] ][ ch[pre[y]][right]==y ] = x;
	pre[x] = pre[y];
	ch[x][type] = y;
	pre[y] = x;
}
void ST::zig(int x){
	// x作为root的左子树
	int y = pre[x];
	ch[y][left] = ch[x][right];
	// 若不是root还有考虑ch[ pre[y] ][ pre[y][1]==y ]
	ch[x][right] = y;
	pre[x] = pre[y];
	pre[y] = x;
}
void ST::zag(int x){
	// x作为root的右子树
	int y = pre[x];
	ch[y][right] = ch[x][left];
	// 若不是root还有考虑ch[ pre[y] ][ pre[y][1]==y ]
	ch[x][left]  = y;
	pre[x] = pre[y];
	pre[y] = x;
}
void ST::Splay(int r, int goal){
	// 著名的伸展操作
	// 将节点r旋转到goal下面

	//        6                        2
	//       / \				      / \
	//      4   7				     1   4
	//     / \		Splay(6,0)<=    /   / \
	//    2   5		=>Splay(2,0)   0   3   6
	//   / \					          / \
	//  1   3					         5   7
	// /
	//0

	while(pre[r] != goal){
		if(pre[ pre[r] ] == goal){ // 如果下一步就是目标位置的下面
			this->Rotate(r, ch[pre[r]][left]==r);
		}else{
			int y = pre[r] , z = pre[y];
			int type = (ch[z][left] == y);
			if(ch[y][type] == r){ 
				// 全速往上
				this->Rotate(r,!type);
				this->Rotate(r,type);
			}else{
				// 快到顶了
				this->Rotate(y,type);
				this->Rotate(r,type);
			}
		}
	}
	if(goal == 0) // 如果转到了root
		root = r;
}

int ST::Insert(int k){
	// 首先通过查找操作确定当前需要插入点的位置,
	// 然后判断插入其左子树或者右子树,
	// 最后不要忘记对插入点进行伸展操作。
	
	int r = root;
	while(ch[r][ key[r]<k ]){ // 找位置
		if(key[r] == k){ // 不重复的插入
			this->Splay(r,0);
			return 0;
		}
		r = ch[r][ key[r]<k ];
	}
	this->NewNode(ch[r][ key[r]<k ], r, k);
	this->Splay(ch[r][ key[r]<k ], 0);
	return k;
}
int ST::GetLess(int x){
	// 直接找左子树的最右边
	int tmin = ch[x][0];
	if(!tmin)return (1<<30);
	while(ch[tmin][1])
		tmin = ch[tmin][1];
	return key[x] - key[tmin];
}
int ST::GetBigger(int x){
	// 如果node 右子树为空,为什么不往上面找找?

	int tmax = ch[x][1];
	if(!tmax)return (1<<30);
	while(ch[tmax][0])
		tmax = ch[tmax][0];
	return key[tmax] - key[x];
}

int main(){
	freopen("in.txt","r",stdin);
	int n;
	while(~scanf("%d",&n)){
		ST s;
		int num,ans = 0;
		for(int i = 1 ; i <= n ; i++){
			if(scanf("%d",&num)==EOF) num = 0;
			if(i == 1){ // 如果第一个节点
				ans += num;
				s.NewNode(s.root,0,num);
			}else{
				if(s.Insert(num)==0)continue;
				int a = s.GetBigger( (int)s.root);
				int b = s.GetLess( (int)s.root);
				ans += min(a,b);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值