码迷,mamicode.com
首页 > 其他好文 > 详细

普通平衡树 splay

时间:2020-04-28 23:29:56      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:oid   man   return   pre   read   while   char   map   regex   

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
	char ch = getchar(); ll x = 0, f = 1;
	while (ch<‘0‘ || ch>‘9‘) { if (ch == ‘-‘)f = -1; ch = getchar(); }
	while (ch >= ‘0‘ && ch <= ‘9‘) { x = x * 10 + ch - ‘0‘; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 5e5 + 10;
int root, tot;
struct node {
	int fa, ch[2], sz, cnt, val;
}tree[N];
void pushup(int rt)
{
	tree[rt].sz = tree[tree[rt].ch[0]].sz + tree[tree[rt].ch[1]].sz + tree[rt].cnt;
}
bool ident(int u, int fa)
{
	return tree[fa].ch[1] == u;
}
void connet(int u, int fa, int son)
{
	tree[u].fa = fa;
	tree[fa].ch[son] = u;
}
void rotate(int u)
{
	int f = tree[u].fa; int ff = tree[f].fa;
	int isson = ident(u, f);
	connet(u, ff, ident(f, ff));
	connet(tree[u].ch[isson ^ 1], f, isson);
	connet(f, u, isson ^ 1);
	pushup(f); pushup(u);
}
void splay(int u, int goal)
{
	while (tree[u].fa != goal)
	{
		int f = tree[u].fa;
		int ff = tree[f].fa;
		if (ff != goal)
			(ident(f, ff) ^ ident(u, f)) ? rotate(u) : rotate(f);
		rotate(u);
	}
	if (goal == 0)
		root = u;
}
void insert(int x)
{
	int u = root; int f = 0;
	while (u&&tree[u].val != x)
	{
		f = u;
		u = tree[u].ch[tree[u].val < x];
	}
	if (u)
	{
		tree[u].cnt++;
	}
	else {
		u = ++tot;
		if (f)
			tree[f].ch[x > tree[f].val] = u;
		tree[u].cnt = 1;
		tree[u].ch[0] = tree[u].ch[1] = 0;
		tree[u].fa = f;
		tree[u].val = x;
		tree[u].sz = 1;
	}
	splay(u, 0);
}
void Find(int x)
{
	int u = root;
	if (!u)return;
	while (tree[u].ch[x > tree[u].val] && x != tree[u].val)
		u = tree[u].ch[x > tree[u].val];
	splay(u, 0);
}
int Next(int x, int f)
{
	Find(x);
	int u = root;
	if ((tree[u].val > x&&f) || (tree[u].val < x && !f))return u;
	u = tree[u].ch[f];
	while (tree[u].ch[f ^ 1])u = tree[u].ch[f ^ 1];
	splay(u, 0);
	return u;
}
void del(int x)
{
	int pre = Next(x, 0);
	int suf = Next(x, 1);
	splay(pre, 0); splay(suf, pre);
	int aim = tree[suf].ch[0];
	if (tree[aim].cnt > 1)tree[aim].cnt--, splay(aim, 0);
	else {
		tree[suf].ch[0] = 0;
	}
}
int K_th(int x)
{
	int u = root;
	if (tree[u].sz < x)return -1;
	while (123)
	{
		int v = tree[u].ch[0];
		if (x > tree[v].sz + tree[u].cnt)
		{
			x -= tree[v].sz + tree[u].cnt;
			u = tree[u].ch[1];
		}
		else {
			if (tree[v].sz >= x)
			{
				u = v;
			}
			else {
				return tree[u].val;
			}
		}
	}
}
int n, opt, x;
int main()
{
	n = read();
	insert(-2147483647);
	insert(+2147483647);
	upd(i, 1, n)
	{
		opt = read();
		if (opt == 1)
		{
			x = read(), insert(x);
		}
		else if (opt == 2)
		{
			x = read(), del(x);
		}
		else if (opt == 3)
		{
			x = read(), Find(x), printf("%d\n", tree[tree[root].ch[0]].sz);
		}
		else if (opt == 4)
		{
			x = read();
			int p = K_th(x + 1);
			printf("%d\n", p);
		}
		else if (opt == 5)
		{
			x = read();
			printf("%d\n", tree[Next(x, 0)].val);
		}
		else {
			x = read(); printf("%d\n", tree[Next(x, 1)].val);
		}
	}
	return 0;
}

普通平衡树 splay

标签:oid   man   return   pre   read   while   char   map   regex   

原文地址:https://www.cnblogs.com/LORDXX/p/12797921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!