treap

21 篇文章 0 订阅
1 篇文章 0 订阅

写了treap..总算是过了。。

先写个递归版的非class版练练手,过几天写一个class当模板

treap=tree+heap

对于每一个,都给一个键值key,要求这个BST在保持BST性质的同时在key上也保持一个heap性质,这样就可以防止一大条链的情况

旋转和正常的差不多。。

在SPOJ3273上跑了4s+[SPOJ真TM慢。。]

#include<cstdio>
#include<ctime>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;

struct node{
	node *lson,*rson;
	int value,key,size;
	node(int v) :value(v),key(rand()),size(1),lson(NULL),rson(NULL){};
	void maintain(){
		if (!this) return;
		size=1;
		if (lson!=NULL) size+=lson->getsize();
		if (rson!=NULL) size+=rson->getsize();
	}
	int getsize(){
		return this?size:0;
	}
};

inline void rotate_l(node* &root){
	node* tmp=root->rson;
	root->rson=tmp->lson;
	tmp->lson=root;
	tmp->maintain();
	root->maintain();
	root=tmp;
}

inline void rotate_r(node* &root){
	node* tmp=root->lson;
	root->lson=tmp->rson;
	tmp->rson=root;
	tmp->maintain();
	root->maintain();
	root=tmp;
}

inline void forever(node* &root,const int &value){
	if (root==NULL){
		root=new node(value);
		return;
	}
	if (value==root->value) return;
	if (value<root->value){
		forever(root->lson,value);
		if (root->lson->key>root->key)
			rotate_r(root);
	}
	else{
		forever(root->rson,value);
		if (root->rson->key>root->key)
			rotate_l(root);
	}
	root->maintain();
}

inline void forget(node* &root,const int &value){
	if (root==NULL) return;
	if (value==root->value){
		if (root->lson && root->rson){
			if (root->lson->key>root->rson->key){
				rotate_r(root);
				forget(root->rson,value);
			}
			else{
				rotate_l(root);
				forget(root->lson,value);
			}
		}
		else{
			node* tmp=root;
			root=root->rson?root->rson:root->lson;
			delete tmp;
			return;
		}
	}
	else
		if (value<root->value)
			forget(root->lson,value);
		else
			forget(root->rson,value);
	root->maintain();
}

inline node* kth(node* root,int k){
	if (root==NULL) return NULL;
	int tmp=root->lson->getsize();
	if (k-1==tmp) 
		return root;
	else
		if ((k-1)<tmp)
			return kth(root->lson,k);
		else
			return kth(root->rson,k-tmp-1);
}

inline int rank(node* root,const int &value){
	if (root==NULL) return 0;
	int tmp=root->lson->getsize();
	if (value==root->value)
		return tmp+1;
	else
		if (value<root->value)
			return rank(root->lson,value);
		else 
			return rank(root->rson,value)+tmp+1;
}

node *root;
int n,t;
char a,str[20];

inline void write(int k){
	if (k>root->getsize())
		printf("invalid\n");
	else
		printf("%d\n",kth(root,k)->value);
}

int main(){
	freopen("treap.in","r",stdin);
	freopen("treap.out","w",stdout);

	srand(unsigned(time));
	scanf("%d\n",&n);
	for (int i=1;i<=n;i++){
        scanf("%s%d",str,&t);
//        cout<<i<<"OK!\n";
//		cout<<i<<' '<<a<<endl;
//		cout<<"if "<<i<<" is a null"<<(root?1:0)<<endl;
		a=str[0];
		if (a=='I')
			forever(root,t);
		if (a=='D')
			forget(root,t);
		if (a=='K')
			write(t);
		if (a=='C')
			printf("%d\n",rank(root,t-1));
//		cout<<root->getsize()<<endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值