王八探头,必有缘由,左偏树第四个点MLE
查看原帖
王八探头,必有缘由,左偏树第四个点MLE
238015
改名但没卡67楼主2021/8/16 12:09
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define ls lson[x]
#define rs rson[x]
using namespace std;
const int kM = 1e6 + 10;
int n, m;
int a[kM], dis[kM], root[kM], rson[kM], lson[kM];
int Merge(int x, int y)
{
	if(!x || !y)
	{
		return x + y;
	}
	if(a[x] > a[y] || (a[x] == a[y] && x > y))
	{
		swap(x, y);
	}
	rs = Merge(rs, y);
	if(dis[ls] < dis[rs])
	{
		swap(ls, rs);
	}
	root[ls] = root[rs] = root[x];
	dis[x] = dis[rs] + 1;
	return x;
}
int Find(int x)
{
	if(root[x] != x)
	{
		root[x] = Find(root[x]);
	}
	return root[x];
}
void Pop(int x)
{
	a[x] = -1;
	root[ls] = ls;
	root[rs] = rs;
	root[x] = Merge(ls, rs);
}
int main()
{
	scanf("%d", &n);
	memset(dis, -1, sizeof(dis));
	for(int i = 1; i <= n; i++)
	{
		root[i] = i;
		scanf("%d", &a[i]);
	}
	int x, y;
	char opt;
	scanf("%d", &m);
	for(int i = 1; i <= m; i++)
	{
		cin>>opt;
		if(opt == 'M')
		{
			scanf("%d%d", &x, &y);
			if(a[y] == -1 || a[x] == -1)
			{
				continue;
			}
			int f1 = Find(x), f2 = Find(y);
			if(x != y)
			{
				root[f1] = root[f2] = Merge(f1, f2);
			}
		}
		else
		{
			scanf("%d", &x);
			if(a[x] == -1)
			{
				printf("0\n");
				continue;
			}
			else
			{
				printf("%d\n", a[Find(x)]);
				Pop(Find(x));
			}
		}
	}
	return 0;
}
/*
5 5
1 5 4 2 3
1 1 5
1 2 5
2 2
1 4 2
2 2
*/
2021/8/16 12:09
加载中...