【BZOJ 1455】罗马游戏

左偏树模板

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1000003;
void read(int &k) {
	k = 0; int fh = 1; char c = getchar();
	for(; c < '0' || c > '9'; c = getchar())
		if (c == '-') fh = -1;
	for(; c >= '0' && c <= '9'; c = getchar())
		k = (k << 1) + (k << 3) + c - '0';
	k = k * fh;
}

bool die[N];
int fa[N], n, m;
int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
struct LeftT {int l, r, v, d;} T[N];
int merge(int x, int y) {
	if (!x) return y; if (!y) return x;
	if (T[x].v > T[y].v) swap(x, y);
	T[x].r = merge(T[x].r, y);
	if (T[T[x].l].d < T[T[x].r].d) swap(T[x].l, T[x].r);
	T[x].d = T[T[x].r].d + 1;
	return x;
}

int main() {
	read(n);
	for(int i = 1; i <= n; ++i)
		{read(T[i].v); fa[i] = i;}
	T[0].d = -1;
	
	char c[10]; int x, y, fx, fy, now;
	read(m);
	while (m--) {
		scanf("%s", c);
		if (c[0] == 'M') {
			read(x); read(y);
			if (die[x] || die[y])
				continue;
			fx = find(x); fy = find(y);
			if (fx != fy) {
				now = merge(fx, fy);
				fa[fx] = fa[fy] = now;
			}
		} else {
			read(x);
			if (die[x]) puts("0");
			else {
				fx = find(x);
				die[fx] = 1;
				printf("%d\n", T[fx].v);
				fa[fx] = merge(T[fx].l, T[fx].r);
				fa[fa[fx]] = fa[fx];
			}
		}
	}
	
	return 0;
}

啦啦啦~

posted @ 2016-05-09 16:46  abclzr  阅读(157)  评论(0编辑  收藏  举报