关于空间。。。
查看原帖
关于空间。。。
124683
Krystallos楼主2020/5/22 10:59

我这个人怎么老是容易MLE。。。代码如下

#include <bits/stdc++.h>
using namespace std;
const int nn = 1e5 + 5;
struct leftistnode {
	int val, ls, rs, id, dis;
} node[nn];
int n, m;
int father[nn];
bool del[nn];
int getfather(int p) {
	return father[p] == p ? p : father[p] = getfather(father[p]);
}
void merge(int p, int q) {
	father[getfather(p)] = getfather(q);
}
int leftistmerge(int p, int q) {
	if (!p || !q)
		return p | q;
	if (node[p].val == node[q].val ? node[p].id > node[q].id : node[p].val > node[q].val)
		swap(p, q);
	node[p].rs = leftistmerge(node[p].rs, q);
	if (node[node[p].ls].dis < node[node[p].rs].dis)
		swap(node[p].ls, node[p].rs);
	node[p].dis = node[node[p].rs].dis + 1;
	return p;
}

int main() {
	scanf("%d %d", &n, &m);
	for (register int i = 1; i <= n; ++i) {
		scanf("%d", &node[i].val);
		node[i].id = i;
		father[i] = i;
	}
	int op, p, q;
	while (m--) {
		scanf("%d %d", &op, &p);
		if (op == 1) {
			scanf("%d", &q);
			p = getfather(p);
			q = getfather(q);
			father[p] = father[q] = leftistmerge(p, q);
		} else {
			if (del[p]) {
				puts("-1");
				continue;
			}
			p = getfather(p);
			printf("%d\n", node[p].val);
			father[node[p].ls] = father[node[p].rs] = father[p] = leftistmerge(node[p].ls, node[p].rs);
			node[p].ls = node[p].rs = 0;
			del[p] = true;
		}
	}
	return 0;
}
2020/5/22 10:59
加载中...