[SPOJ ORDERSET] Order statistic set [Splay]

本文介绍了一种基于SplayNode的平衡树实现方法,并详细展示了如何通过该结构完成数字的插入、删除、查询等核心操作。此外,还提供了完整的源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一棵平衡树,要求实现以下操作,插入、删除数字,查询某个数字是第几大的,查询第k大的数字是几。

#include <cstdio>

const int MAXINT=~0u>>1;

struct SplayNode {
	SplayNode *ls,*rs,*f;
	int x,size;
	SplayNode *clear(int xx,SplayNode *ff=NULL) {
		f=ff;
		x=xx;
		ls=rs=NULL;
		size=1;
		return this;
	}
	void repairSize() {
		size=1;
		if (ls) size+=ls->size;
		if (rs) size+=rs->size;
	}
	void rot() {
		SplayNode *x=this,*y=this->f;
		if (x==y->ls) {
			y->ls=x->rs;
			x->rs=y;
			if (y->ls) y->ls->f=y;
		} else {
			y->rs=x->ls;
			x->ls=y;
			if (y->rs) y->rs->f=y;
		}
		x->f=y->f;
		y->f=x;
		if (x->f) 
			if (x->f->ls==y) x->f->ls=x;
			else x->f->rs=x;
		y->repairSize();
		x->repairSize();
	}
	int dir() {
		if (this->f)
			if (this==this->f->ls) return -1;
			else return 1;
		return 0;
	}
};
SplayNode b[200010],*bp,*root;
void print(int root) {
	printf("Root: %d\n",root);
	for (SplayNode *cur=b;cur!=bp;cur++) {
		printf("Node %d:\n",(int)(cur-b));
		printf("  ls:%d rs:%d f:%d\n",(int)(cur->ls-b),(int)(cur->rs-b),(int)(cur->f-b));
		printf("  x:%d size:%d\n",cur->x,cur->size);
		int x,num,size;
	}
}
SplayNode *splay(SplayNode *x,SplayNode *f=NULL) {
	while (x->f!=f) {
		if (x->f->f==f) x->rot();
		else if (x->dir()==x->f->dir()) {
			x->f->rot();
			x->rot();
		} else {
			x->rot();
			x->rot();
		}
	}
	return x;
}
SplayNode *less(SplayNode *root,int x) {
	SplayNode *ans=NULL;
	for (SplayNode *cur=root;cur;) {
		if (cur->x>=x) cur=cur->ls;
		else {
			ans=cur;
			cur=cur->rs;
		}
	}
	return splay(ans,root->f);
}
SplayNode *greater(SplayNode *root,int x) {
	SplayNode *ans=NULL;
	for (SplayNode *cur=root;cur;) {
		if (cur->x<=x) cur=cur->rs;
		else {
			ans=cur;
			cur=cur->ls;
		}
	}
	return splay(ans,root->f);
}
void insert(int x) {
	root=less(root,x);
	root->rs=greater(root->rs,x);
	if (root->rs->ls!=NULL) return;
	root->rs->ls=(bp++)->clear(x,root->rs);
	root->rs->size++;
	root->size++;
}
void erase(int x) {
	root=less(root,x);
	root->rs=greater(root->rs,x);
	if (root->rs->ls==NULL) return;
	root->rs->ls=NULL;
	root->rs->size--;
	root->size--;
}
int kth(int k) {
	if (k>root->size) return MAXINT;
	for (SplayNode *cur=root;cur;) {
		if (cur->ls) {
			if (k<=cur->ls->size) {
				cur=cur->ls;
				continue;
			} else k-=cur->ls->size;
		}
		if (k==1) {
			root=splay(cur,root->f);
			return cur->x;
		} else k--;
		cur=cur->rs;
	}
}
int count(int x) {
	root=less(root,x);
	root->rs=greater(root->rs,x);
	if (root->ls) return root->ls->size+1;
	else return 1;
}
void clear() {
	bp=b;
	root=(bp++)->clear(-MAXINT);
	root->rs=(bp++)->clear(MAXINT,root);
	root->size++;
}

int main() {
	int q,x;
	char c;
	scanf("%d",&q);
	clear();
	while (q--) {
		scanf(" %c%d",&c,&x);
		if (c=='I') insert(x);
		else if (c=='D') erase(x);
		else if (c=='K') {
			int ans=kth(x+1);
			if (ans==MAXINT) printf("invalid\n");
			else printf("%d\n",ans);
		} else {
			printf("%d\n",count(x)-1);
		}
		//print(root-b);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值