SBT平衡树

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++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()))
#define ALL(x) (x).begin(), (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 = 300100, oo = 0x3f3f3f3f;

int n, m;

#define L ch[0]
#define R ch[1]

struct node
{
    int x, size;
    node *f, *ch[2];
    node()
    {
		x = size = 0;
        f = L = R = NULL;
    }
};

node *Null;

inline void init()
{
	Null = new node;
	Null->f = Null->L = Null->R = Null;
	Null->size = 0;
}

inline node *new_node(int k)
{
	node *tmp = new node;
	tmp->x = k;
	tmp->size = 1;
	tmp->f = tmp->L = tmp->R = Null;
    return tmp;
}

struct SBT
{
    node *root = NULL;
    inline bool is_root(node *t) { return t == Null || t->f->L != t && t->f->R != t; }
    inline void push_up(node *t) { t->size = t->L->size + t->R->size + 1; }
    inline void rotate(node *x)
    {
		if (is_root(x)) return;
        node *y = x->f;
        if (is_root(y))
            root = x;
        else{
            if (y == y->f->L)
                y->f->L = x;
            else y->f->R = x;
        }
        x->f = y->f;
        int k = x == y->R;
        y->ch[k] = x->ch[!k];
        if (x->ch[!k] != Null) x->ch[!k]->f = y;
        x->ch[!k] = y;
        y->f = x;
        push_up(y);
        push_up(x);
	}
	void maintain(node *t, int T)
	{
		if (!T)
		{
			if (t->L->L->size > t->R->size)
				rotate(t->L);
			else if(t->L->R->size > t->R->size)
			{
				rotate(t->L->R);
				rotate(t->L);
			} else return;
		} else {
			if (t->R->R->size > t->L->size)
				rotate(t->R);
			else if (t->R->L->size > t->L->size)
			{
				rotate(t->R->L);
				rotate(t->R);
			} else return;
		}
		maintain(t->L, 0);
		maintain(t->R, 1);
		maintain(t, 0);
		maintain(t, 1);
	}
	void insert(int k, node *t)
	{
		if (root == NULL)
		{
			root = new_node(k);
			return;
		}
		int T = k >= t->x;
		if (t->ch[T] != Null)
			insert(k, t->ch[T]);
		else {
			t->ch[T] = new_node(k);
			t->ch[T]->f = t;
		}
		push_up(t);
		maintain(t, T);
	}
	void dfs(node *x)
	{
		if (x == Null) return;
		dfs(x->L);
		printf("%d ", x->x);
		dfs(x->R);
	}
	inline void out()
	{
		dfs(root);
		puts("");
	}
}t;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	init();
	scanf("%d", &n);
	REP(i, 1, n)
	{
		int k;
		scanf("%d", &k);
		t.insert(k, t.root);
	}
	t.out();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值