Splay平衡树 (LCT基础)

#include<bits/stdc++.h>

using namespace std;

#define REP(i,j,k) for (int i = (j), _end_ = (k); i <= _end_; ++i)
#define DREP(i,j,k) for (int i = (j), _start_ = (k); i >= _start_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()-1))
#define ALL(x) ((x).begin()+1), (x).end()

template<typename T> inline bool chkmin(T &a,const T &b){ return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a,const T &b){ return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int dmax=100100,oo=0x3f3f3f3f;

struct node{
	int x;
	node *f,*l,*r;
	node(){ x=0,f=l=r=NULL; }
};

node *root;
void left(node *x){
	node *y=x->f;
	if (y==root)
		root=x;
	y->l=x->r;
	if (x->r!=NULL)
		x->r->f=y;
	x->f=y->f;
	if (y->f!=NULL)
		if (y==y->f->l)
			y->f->l=x;
		else y->f->r=x;
	x->r=y;
	y->f=x;
}
void right(node *x){
	node *y=x->f;
	if (y==root)
		root=x;
	y->r=x->l;
	if (x->l!=NULL)
		x->l->f=y;
	x->f=y->f;
	if (y->f!=NULL)
		if (y==y->f->l)
			y->f->l=x;
		else y->f->r=x;
	x->l=y;
	y->f=x;
}
void rotate(node *t){
	node *x=t->f,*y=t->f->f;
	if (x==root){
		if (x->l==t)
			left(t);
		else right(t);
	}else{
		if (x==y->l){
			if (t==x->l){
				left(x);
				left(t);
			}else{
				right(t);
				left(t);
			}
		}else{
			if (t==x->r){
				right(x);
				right(t);
			}else{
				left(t);
				right(t);
			}
		}
	}
}
void push(node *t,int k){
	node *tmp=new node;
	if (k<t->x)
		t->l=tmp;
	else t->r=tmp;
	tmp->f=t;
	tmp->x=k;
	while (tmp!=root)
		rotate(tmp);
}
void insert(node *t,int k){
	if (root==NULL){
		root=new node;
		root->x=k;
		return;
	}
	if (k<t->x){
		if (t->l==NULL)
			push(t,k);
		else insert(t->l,k);
	}else{
		if (t->r==NULL)
			push(t,k);
		else insert(t->r,k);
	}
}
void create(int x){ insert(root,x); }
bool find(node *t,int k){
	if (k==t->x)
		return 1;
	if (k<t->x){
		if (t->l==NULL)
			return 0;
		else return find(t->l,k);
	}else{
		if (t->r==NULL)
			return 0;
		else return find(t->r,k);
	}
}
bool check(int x){ return find(root,x); }
void dfs(node *t){
	if (t->l!=NULL) dfs(t->l);
	printf("%d ",t->x);
	if (t->r!=NULL) dfs(t->r);
}
void out(){ dfs(root),puts(""); }

int n,m;

int main(){
	scanf("%d",&n);
	REP(i,1,n){
		int k;
		scanf("%d",&k);
		create(k);
	}
	out();
	scanf("%d",&m);
	REP(i,1,m){
		int k;
		scanf("%d",&k);
		if (check(k))
			puts("True");
		else puts("False");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值