SplayTree

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef int Int
struct Node{
	int size,padir,reverse;
};
struct Stack{
	int top; Stack():top(0){}
	Node * node[maxn]; 
	Node * operator [](const int i)const{ return node[i]; }
}stack;
void Clear(Node * &root){ root=nil; root->size=0; }
Node * CreatNode(){ 
	Node * node=new Node(); node->padir=2; node->size=1; node->reverse=0;
	node->pa=node->chi[0]->chi[1]=nil; return node;
}
void Update(Node * node){
	node->size=node->chi[0]->size+1+node->chi[1]->size;
}
void Pushdown(Node * node){
	if(node->reverse){
		Swap(node->chi[0],node->chi[1]); // include padir
		node->chi[0]->reverse^=1; node->chi[1]->reverse^=1;
		node->reverse=0;
	}
}
void Rotate(Node * node){
	...  Update(node);
}
void Splay(Node * node){
	stack.top=0; 
	for(Node * o=node;o!=nil;o=o->pa) stack[stack.top++]=o;
	for(int i=stack.top-1;i>=0;i--) Pushdown(stack[i]);
	while(node!=root){
		Node *pa=x->pa; 
		if(node->padir^pa->padir) Rotate(pa,pa->padir);
		else Rotate(pa->pa,pa->dir);
		if(node==root) break; Rotate(node,node->padir^1);
	}
	Update(node);
}
Node * Search(Node * node,int rank){
	while(1){
		Pushdown(node);
		if(node->chi[0]->size+1==rank) return node;
		if(rank<=node->chi[0]->size) node=node->chi[0];
		else{ rank-=node->chi[0]->size+1; node=node->chi[1]; }
	}
}
void Split(Node * &root,Node * &node,int rank){
	Splay(node=Search(root,rank+1));
	root=node->chi[0]; root->padir=2; node->chi[0]=nil; Update(node);
}
void Split(Node * &root,Node * &lhd,Node * &rhd,int lst,int rst)
{ Split(root,rhd,rst); Split(root,lhd,lst); }
void Merge(Node * &root,Node * node)
{ Splay(root=Search(root,root->size)); Connect(root,node,1); Update(root); }
void Merge(Node * &root,Node * x,Node * y)
{ Merge(x,y); Merge(root,x); }
int GetRank(Node * node){
	Splay(node); root=node; return node->chi[0]->size+1;
}
void Reverse(int lst,int rst){
	Node * x,y;
	Split(root,x,y,lst,rst+1);
	x.reverse^=1; Merge(root,x,y);
}
/*void Splay(Node * &node,Int key){
	int dir=(key==node->key)?0:(key<node->key?-1:1);
	if(d==1) key-=
}*/

int main(){
	return 0;
}

 

posted on 2013-08-21 15:09  Amyc  阅读(249)  评论(0编辑  收藏  举报