Begins and tests

CSDN.....

以后就在这里写一些关于OI的东西了...

高亮测试!


/*
Spaly Templete
*/


#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

const int INF=(1<<30)-1;

class node
{
	public:
	long long tot,v;
	node *pre;
	node *son[2]; //0:left 1:right
};

node t[10000000];
int nodetot=0;

node*nil=new node;
node* root;

node* newnode()
{ 
	t[nodetot].son[1]=t[nodetot].son[0]=t[nodetot].pre=nil; 
	t[nodetot].tot=0; 
	return &t[nodetot++]; 
}
void update(node*x)
{ x->tot = x->son[0]->tot + x->son[1]->tot + 1 ; }


void Rotate(node*x,int k) //0:left->. 1:right->.
{
	if(x->pre==nil)return;
	node* y=x->pre;
	
	if(x->son[k^1]!=nil)
		x->son[k^1]->pre=y;
	y->son[k]=x->son[k^1];
	
	if(y==y->pre->son[0])
		y->pre->son[0]=x;
	else if(y==y->pre->son[1])
		y->pre->son[1]=x;
	x->pre=y->pre;
	
	x->son[k^1]=y;
	y->pre=x;
	
	update(y);
	update(x);
}


void Splay(node*x,node*f)
{
	while(x->pre != f)
	{
		node*y=x->pre;
		
		if(y->pre == f)
		{
			if(x == x->pre->son[0])
				Rotate(x,0);
			else if(x == x->pre->son[1])
				Rotate(x,1);
			break;
		}
		
		if(x==y->son[0])
			if(y==y->pre->son[0])
		 		Rotate(y,0),Rotate(x,0);
		 	else
		 		Rotate(x,0),Rotate(x,1);
 		else
 			if(y==y->pre->son[0])
 				Rotate(x,1),Rotate(x,0);
			else
 				Rotate(y,1),Rotate(x,1);
		
	}
	if(f==nil)root=x;
}

void Splay(node*x) { Splay(x,nil); }

void Insert(int k)
{
	if(root==nil) 
	{
		root=newnode();
		root->pre=nil;
		root->v=k;
		root->tot=1;
		return ;
	}
	
	node*x=root;
	while(true)
	{
		x->tot+=1;
		if(k>x->v) //right
		{
			if(x->son[1]==nil)
			{
				x->son[1]=newnode();
				x->son[1]->pre=x;
				x->son[1]->v=k;
				x->son[1]->tot=1;
				Splay(x->son[1]);
				break;
			}else x=x->son[1];
		}else //k<=x->v,left
		{
			if(x->son[0]==nil)
			{
				x->son[0]=newnode();
				x->son[0]->pre=x;
				x->son[0]->v=k;
				x->son[0]->tot=1;
				Splay(x->son[0]);
				break;
			}else x=x->son[0];
		}
	}
} 

node* Find(int k)
{
	if(root==nil)
	{
		cout<<"find from empty tree;"<<endl;
		return nil;
	}
	node*x=root;
	while(true)
	{
		if(x->v==k)return x;
		else
		if(k>x->v) //right
		{
			if(x->son[1]==nil) return nil;
			x=x->son[1];
		}
		else //left
		{
			if(x->son[0]==nil) return nil;
			x=x->son[0];
		}
	}
	
	return nil;
}

node* Prefix(int k)
{
	node*x=Find(k);
	if(x==nil) return nil;
	Splay(x);
	if(x->son[0]==nil) return nil;
	x=x->son[0];
	while(x->son[1]!=nil)x=x->son[1];
	return x;
}

node* Suffix(int k)
{
	node*x=Find(k);
	if(x==nil) return nil;
	Splay(x);
	if(x->son[1]==nil) return nil;
	x=x->son[1];
	while(x->son[0]!=nil)x=x->son[0];
	return x;
}

void Remove(int k)
{
	node*x=Find(k);
	if(x==nil)return ;
	
	node* L=Prefix(k);
	node* R=Suffix(k);
	if(L==nil && R==nil)
	{
		root=nil;
		return ; 
	}else if(L==nil)
	{
		Splay(R);
		R->son[0]=nil;
		update(R);
	}else if(R==nil)
	{
		Splay(L); 
		L->son[1]=nil;
		update(L);
	}else
	{
		Splay(L);
		Splay(R,L);
		R->son[0]=nil;
		update(R);
		update(L); 
	}
}

int Rank(int k)
{
	if(root==nil)return -1;
	node*x=Find(k);
	if(x==nil)return -1;
	Splay(x);
	return x->son[0]->tot;
}

int RevRank(int k)
{
	if(root==nil)return -1;
	node*x=Find(k);
	if(x==nil)return -1;
	Splay(x);
	return x->son[1]->tot;
}


void __Output(node*i)
{
	//cout<<i<<' '<<i->son[0]<<' '<<i->son[1]<<endl; 
	if(i==nil)return ;
	__Output(i->son[0]);
	//cout<<i->v<<"("<<i->tot<<")"<<' ';
	cout<<i->v<<' ';
	__Output(i->son[1]);
}
void Output()
{ __Output(root); }

int pos=0;
void __Output(node*i,int*x)
{
	if(i==nil)return ;
	__Output(i->son[0],x);
	x[pos++]=i->v;
	__Output(i->son[1],x);
}
void Output(int*x)
{ __Output(root,x); }



int q[1000000];
int f[1000000];

int n,m;

;int main()
{
	nil->son[0]=nil->son[1]=nil->pre=nil;
	nil->tot=0;
	root=nil;
	
	//test();
	
	cin>>n>>m;
	
	
	
	for(int i=0;i<n;i++)
	{
		int c;
		cin>>c;
		int a;
		switch(c)
		{
			default:break;
			
			case 1://insert
				cin>>a;
				Insert(a);
			break;
			
			case 2://query rank
				cin>>a;
				cout<<"Rank:"<<Rank(a)<<endl;
			break;
			 
			case 3://query prefix
				cin>>a;
				cout<<"Prefix:"<<(Prefix(a)->v)<<endl; 
			break;
			
			case 4://query suffix
				cin>>a;
				cout<<"Suffix:"<<(Suffix(a)->v)<<endl; 
			break;
			
			case 5://query exist;
				cin>>a;
				cout<<(Find(a)!=nil ? "Exist." : "Not Exist.")<<endl;
			break;
			
			case 6://delete
				cin>>a;
				Remove(a);
			break;
			
			case 7://output
		 		cout<<"[ ";
		 		cout<<(root==nil ? "Empty tree;" : (Output(),""));
		 		cout<<"]"<<endl;
			break;
			
			
		};
		
	}
	
	
	
	return 0;
}

 

效果不错......

删掉原程序中的一些注释.

不知道审核要多久.

以后这里作为代码仓库和复习提纲的存放处好了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值