zoj 3612 Median/sbt

原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4736

SBT树的简单运用,测试了重复元素的插入删除过程。

具体如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef long long ll;
#define Max_N 10100
#define size(_) ((_)==NULL ? 0 : (_)->size)
typedef struct sbt{
	ll key;
	int cnt, size;
	struct sbt *ch[2];
}BSTNode, *SBT;
BSTNode stack[Max_N];
int sz = 0;
void rotate(SBT *x, int d){
	SBT k = (*x)->ch[!d];
	(*x)->ch[!d] = k->ch[d];
	k->ch[d] = *x;
	k->size = size(*x);
	(*x)->size = size((*x)->ch[0]) + size((*x)->ch[1]) + (*x)->cnt;
	*x = k;
}
void Maintain(SBT *x, int d){
	if ((*x)->ch[d] == NULL) return;
	if (size((*x)->ch[d]->ch[d]) > size((*x)->ch[!d])){
		rotate(x, !d);
	} else if (size((*x)->ch[d]->ch[!d]) > size((*x)->ch[!d])){
		rotate(&((*x)->ch[d]), d), rotate(x, !d);
	} else {
		return;
	}
	Maintain(x, 0), Maintain(x, 1);
}
void insert(SBT *x, ll key){
	if (*x == NULL){
		*x = &stack[sz++];
		(*x)->ch[0] = (*x)->ch[1] = NULL;
		(*x)->key = key, (*x)->size = (*x)->cnt = 1;
	} else {
		(*x)->size++;
		if (key == (*x)->key){
			(*x)->cnt++;
			return;
		}
		insert(&((*x)->ch[key > (*x)->key]), key);
		Maintain(x, key > (*x)->key);
	}
}
ll find_kth(SBT x, int k){
	int t = 0;
	for (; x != NULL;){
		t = size(x->ch[0]);
		if (t + 1 <= k && k <= t + x->cnt) break;
		else if (k < t + 1) x = x->ch[0];
		else k -= t + x->cnt, x = x->ch[1];
	}
	return x->key;
}
void _delete(SBT *x, ll key){
	SBT t = NULL;
	if (*x == NULL) return;
	(*x)->size--;
	if ((*x)->key == key){
		if ((*x)->cnt > 1){
			(*x)->cnt--;
		} else {
			if (!(*x)->ch[0] || !(*x)->ch[1]){
				*x = (*x)->ch[0] ? (*x)->ch[0] : (*x)->ch[1];
			} else {
				t = (*x)->ch[1];
				for (; t->ch[0] != NULL; t = t->ch[0]);
				_delete(&((*x)->ch[1]), (*x)->key = t->key);
			}
		}
	} else {
		_delete(&((*x)->ch[key > (*x)->key]), key);
	}
}
SBT search(SBT root, ll x){
	if (root == NULL) return NULL;
	else if (root->key == x) return root;
	else return search(root->ch[x > root->key], x);
}
void query(SBT root, ll x){
	int t;
	ll v1, v2;
	SBT ret = NULL;
	if (!size(root)){
		printf("Empty!\n");
		return;
	} else {
		t = size(root);
		if (t % 2 != 0){
			printf("%lld\n", find_kth(root, (t >> 1) + 1));
		} else {
			v1 = find_kth(root, t >> 1);
			v2 = find_kth(root, (t >> 1) + 1);
			if ((v1 + v2) % 2 == 0){
				printf("%1ld\n", (v1 + v2) >> 1);
			} else {
				printf("%.1lf\n", (double)(v1 + v2) / 2.0);
			}
		}
	}
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	ll x;
	int T, n;
	char buf[20];
	SBT root = NULL;
	scanf("%d", &T);
	while (T--){
		sz = 0, root = NULL;
		scanf("%d", &n);
		while (n--){
			scanf("%s %lld", buf, &x);
			if (buf[0] == 'a'){
				insert(&root, x);
				query(root, x);
			} else {
				if (!root || !search(root, x)){
					printf("Wrong!\n");
					continue;
				}
				_delete(&root, x);
				query(root, x);
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值