65pts求条 玄2关 WA on#8 ~ #12
查看原帖
65pts求条 玄2关 WA on#8 ~ #12
730195
Little_Cabbage楼主2024/2/23 15:48
#include <bits/stdc++.h>
#define ull unsigned long long
#define int long long
#define ldb long double
#define db double
#define endl '\n'
using namespace std;
const int N = 3e7 + 10;
int read() {
	char c = 0;
	int f = 1, x = 0;
	while (c < '0' || c > '9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		x = x * 10 + (int)(c - '0');
		c = getchar();
	}
	return f * x;
}
void write(int x) {
	if (x == 0) {
		putchar(x + '0');
		return ;
	}
	if (x < 0) {
		putchar('-');
		x = abs(x);
	}
	stack<int> s;
	while (x) {
		s.push(x % 10);
		x /= 10;
	}
	while (s.size()) {
		putchar(s.top() + '0');
		s.pop();
	}
}
int root, n;
#define ls(k) Splay_Tree[k].left_son
#define rs(k) Splay_Tree[k].right_son
#define fa(k) Splay_Tree[k].father
#define lss(k) Splay_Tree[k].left_son_sum
#define rss(k) Splay_Tree[k].right_son_sum
#define va(k) Splay_Tree[k].val
struct SplayTree {
	int left_son, right_son;
	int father;
	int left_son_sum, right_son_sum;
	int val;
} Splay_Tree[N];
void Zig(int x) {
	int y = fa(x);
	ls(y) = rs(x);
	if (rs(x)) fa(rs(x)) = y;
	fa(x) = fa(y);
	if (fa(y)) {
		if (ls(fa(y)) == y)
			ls(fa(y)) = x;
		else
			rs(fa(y)) = x;
	}
	rs(x) = y;
	fa(y) = x;
	lss(y) = rss(x);
	rss(x) = lss(y) + rss(y) + 1;
}
void Zag(int x) {
	int y = fa(x);
	rs(y) = ls(x);
	if (ls(x)) fa(ls(x)) = y;
	fa(x) = fa(y);
	if (fa(y)) {
		if (ls(fa(y)) == y)
			ls(fa(y)) = x;
		else
			rs(fa(y)) = x;
	}
	ls(x) = y;
	fa(y) = x;
	rss(y) = lss(x);
	lss(x) = lss(y) + rss(y) + 1;
}
void Splay(int x) {
	while (fa(x)) {
		int y = fa(x);
		if (!fa(y)) {
			if (x == ls(y))
				Zig(x);
			else
				Zag(x);
			break;
		}
		if (x == ls(y)) {
			if (y == ls(fa(y))) {
				Zig(y);
				Zig(x);
			} else {
				Zig(x);
				Zag(x);
			}
		} else {
			if (y == ls(fa(y))) {
				Zag(x);
				Zig(x);
			} else {
				Zag(y);
				Zag(x);
			}
		}
	}
	root = x;
}
void Insert(int x) {
	int y, f;
	y = root;
	while (y) {
		f = y;
		if (x <= va(y)) {
			lss(y)++;
			y = ls(y);
		} else {
			rss(y)++;
			y = rs(y);
		}
	}
	n++;
	va(n) = x;
	ls(n) = rs(n) = lss(n) = rss(n) = 0;
	if (root == 0) {
		root = n;
		return ;
	}
	fa(n) = f;
	if (x <= va(f))
		ls(f) = n;
	else
		rs(f) = n;
	Splay(n);
}
int Find(int x) {
	int y = root;
	while (y) {
		if (va(y) == x) {
			Splay(y);
			return 1;
		}
		if (x < va(y))
			y = ls(y);
		else
			y = rs(y);
	}
	return 0;
}
void Delete(int x) {
	if (!Find(x)) return ;
//	Find(x);
	int y = root;
	int Ls = ls(y), Rs = rs(y);
	if (!Ls && ! Rs) {
		root = 0;
		return ;
	}
	if (!Ls) {
		root = Rs;
		fa(Rs) = 0;
		return ;
	} else if (!Rs) {
		root = Ls;
		fa(Ls) = 0;
		return ;
	}
	y = Ls;
	fa(Ls) = 0;
	while (rs(y))
		y = rs(y);
	Splay(y);
	rs(y) = Rs, fa(Rs) = y, rss(y) = lss(Rs) + rss(Rs) + 1;
}
int Query_x_rank(int x) {
	Find(x);
	return lss(root) + 1;
}
int Query_rank_x(int x) {
	int y = root;
	if (lss(y) + rss(y) + 1 < x) {
		return -1;
	}
	while (1) {
		if (lss(y) + 1 == x) {
			return va(y);
		}
		if (lss(y) >= x) {
			y = ls(y);
		} else {
			x = x - lss(y) - 1;
			y = rs(y);
		}
	}
}
void dfs1(int k, int x, int &ans) {
	if (!k) return ;
	if (va(k) < x) {
		ans = max(ans, va(k));
	}
	dfs1(ls(k), x, ans);
	dfs1(rs(k), x, ans);
}
void dfs2(int k, int x, int &ans) {
	if (!k) return ;
	if (va(k) > x) {
		ans = min(ans, va(k));
	}
	dfs2(ls(k), x, ans);
	dfs2(rs(k), x, ans);
}
int Pred(int x) {
//	int t = -1e9;
//	dfs1(root, x, t);
//	return t;
	Find(x);
	int y = ls(root);
	while (y) {
		if (rs(y) == 0)
			break;
		y = rs(y);
	}
	if (y)
		return va(y);
	return -1;
}
int Succ(int x) {
//	int t = 1e9;
//	dfs2(root, x, t);
//	return t;
	Find(x);
	int y = rs(root);
	while (y) {
		if (ls(y) == 0)
			break;
		y = ls(y);
	}
	if (y)
		return va(y);
	return -1;
}
int m;
void dfs(int k) {
	if (!k) return ;
	cout << k << " " << ls(k) << " " << rs(k) << " " << fa(k) << " " << va(k) << endl;
	dfs(ls(k));
	dfs(rs(k));
}
void debug() {
	dfs(root);
	cout << "------------------------------------\n";
}
signed main() {
//	freopen("P3369_8.in", "r", stdin);
//	freopen("P3369_8.ans", "w", stdout);
//	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	m = read();
	int t = m, i = 0;
	int tot = 0;
	while (m--) {
		bool flag = false;
//		i++;
		int opt, x;
		opt = read(), x = read();
//		if (opt >= 3) tot++;
//		if (tot == 3870) {
//			tot++;
//			cout << opt << endl;
//		}
//		if (tot == 304 && t == 1000) {
//			cout << "241377\n";
//			t++;
//			continue;
//		}
//		/*
		if (opt == 1) {
			Insert(x);
		} else if (opt == 2) {
			Delete(x);
		} else if (opt == 3) {
			if (!Find(x)) {
				flag = true;
				Insert(x);
			}
			cout << Query_x_rank(x) << endl;
			if (flag)
				Delete(x);
		} else if (opt == 4) {
			cout << Query_rank_x(x) << endl;
		} else if (opt == 5) {
			if (!Find(x)) {
				flag = true;
				Insert(x);
			}
			cout << Pred(x) << endl;
			if (flag)
				Delete(x);
		} else {
			if (!Find(x)) {
				flag = true;
				Insert(x);
			}
			cout << Succ(x) << endl;
			if (flag)
				Delete(x);
		}
//		*/
//		debug();
	}
	return 0;
}

2024/2/23 15:48
加载中...