刚学 Splay,求助!
查看原帖
刚学 Splay,求助!
134000
Plozia楼主2020/12/29 14:56

萌新刚学 Splay,使用非指针结构体写法,但是样例过不去,第二个输出 WA,第三个输出直接死循环,请大佬帮忙查错!谢谢!

//Splay
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 10;
int n, cnt, root = -1;
struct node
{
	int fa, child[2], size, cnt, val;
}tree[MAXN];

int read()
{
	int sum = 0, fh = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') fh = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {sum = (sum << 3) + (sum << 1) + (ch ^ 48); ch = getchar();}
	return sum * fh;
}

bool rson(int x, int f) {return tree[f].child[1] == x;}
void update(int u) {tree[u].size = tree[tree[u].child[0]].size + tree[tree[u].child[1]].size + tree[u].cnt;}

void rotate(int x)
{
	int f = tree[x].fa, gf = tree[f].fa;
	bool p = rson(x, f), q = !p;
	if (gf) tree[gf].child[rson(f, gf)] = x;
	else root = x;
	tree[x].fa = gf;
	tree[f].fa = x;
	tree[f].child[p] = tree[x].child[q];
	tree[tree[x].child[q]].fa = f;
	tree[x].child[q] = f;
	update(f), update(x);
}

void Splay(int x, int target)
{
	while (tree[x].fa != target)
	{
		int f = tree[x].fa, gf = tree[f].fa;
		if (gf == target) {rotate(x); break;}
		if (rson(x, f) == rson(f, gf)) rotate(f);
		else rotate(x);
		rotate(x);
	}
}

void Find(int x)
{
	int now = root;
	while (now)
	{
		if (tree[now].val == x) break;
		if (tree[now].val > x) now = tree[now].child[0];
		else now = tree[now].child[1];
	}
	Splay(now, 0);
}

void Insert(int x)
{
	if (cnt == 0 || root == -1)
	{
		root = ++cnt;
		tree[cnt].size = tree[cnt].cnt = 1;
		tree[cnt].val = x; return;
	}
	int now = 0;
	while (now)
	{
		tree[now].size++;
		if (tree[now].val == x) {tree[now].size++; tree[now].cnt++; return ;}
		bool b = x > tree[now].val;
		if (!tree[now].child[b])
		{
			++cnt; tree[now].child[b] = cnt;
			tree[cnt].size = tree[cnt].val = 1;
			tree[cnt].fa = now; tree[cnt].val = x;
			Splay(cnt, 0); return ;
		}
		now = tree[now].child[b];
	}
}

void Delete(int x)
{
	Find(x);
	if (tree[root].cnt > 1) {tree[root].size--; tree[root].cnt--; return ;}
	if (!tree[root].child[0] && !tree[root].child[1]) {root = -1; return ;}
	if (!tree[root].child[0]) {tree[tree[root].child[1]].fa = 0; root = tree[root].child[1]; return ;}
	if (!tree[root].child[1]) {tree[tree[root].child[0]].fa = 0; root = tree[root].child[0]; return ;}
	int now = tree[root].child[1];
	while (tree[now].child[0]) now = tree[now].child[0];
	Splay(now, root);
	tree[now].fa = 0;
	tree[tree[root].child[0]].fa = now;
	tree[now].child[0] = tree[root].child[0];
	root = now;
	update(root);
}

void Find_Rank_of_x(int x)
{
	Find(x);
	printf("%d\n", tree[tree[root].child[0]].size + 1);
}

void Find_xth(int x)
{
	int now = root;
	while (now)
	{
		if (tree[tree[now].child[0]].size < x && tree[tree[now].child[0]].size + tree[now].cnt >= x) break;
		if (tree[tree[now].child[0]].size >= x) now = tree[now].child[0];
		else {x -= tree[tree[now].child[0]].size + tree[now].cnt; now = tree[now].child[1];}
	}
	printf("%d\n", tree[now].val);
	Splay(now, 0);
}

void Find_pre(int x)
{
	Insert(x);
	int now = tree[now].child[0];
	while (tree[now].child[1]) now = tree[now].child[1];
	printf("%d\n", tree[now].val);
	Delete(x);
}

void Find_aft(int x)
{
	Insert(x);
	int now = tree[now].child[1];
	while (tree[now].child[0]) now = tree[now].child[0];
	printf("%d\n", tree[now].val);
	Delete(x);
}

int main()
{
	n = read();
	while (n--)
	{
		int opt = read(), x = read();
		switch (opt)
		{
			case 1: Insert(x); break;
			case 2: Delete(x); break;
			case 3: Find_Rank_of_x(x); break;
			case 4: Find_xth(x); break;
			case 5: Find_pre(x); break;
			case 6: Find_aft(x); break;
		}
	}
	return 0;
}
2020/12/29 14:56
加载中...