样例未过大佬求条
查看原帖
样例未过大佬求条
538761
huays1230楼主2024/1/2 21:05
#include<bits/stdc++.h>
#define N 114514

int n, m;
bool tf[N];

namespace hys
{
	int fa[N];
	bool vis[N];
	
	struct node
	{
		int v;
		int id;
		int dist;
		int ls, rs;
		
		bool operator< (node x)
		const
		{
			if(v == x.v)
			{
				return id < x.id;
			}
			else
			{
				return v < x.v;
			}
		}
	};
	
	struct node t[N];
	
	int find(int x)
	{
		if(fa[x] == x)
		{
			return x;
		}
		fa[x] = find(fa[x]);
		return fa[x];
	}
	
	int merge(int x, int y)
	{
		if(!x || !y)
		{
			return x + y;
		}
//		if(t[x] > t[y])
		if(t[y] < t[x])
		{
			std::swap(x, y);
		}
		t[x].rs = merge(t[x].rs, y);
		if(t[t[x].rs].dist > t[t[x].ls].dist)
		{
			std::swap(t[x].rs, t[x].ls);
		}
		t[x].dist = t[t[x].rs].dist + 1;
		return x;
	}
}

int main()
{
	int i;
	int op, x, y;
	
//	std::freopen("3377.in", "r", stdin);
//	std::freopen("3377.out", "w", stdout);
	
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	
	hys::t[0].dist = -1;//init
	
	std::cin >> n >> m;
	for(i = 1; i <= n; i++)
	{
		std::cin >> hys::t[i].v;
		hys::fa[i] = i;
		hys::t[i].id = i;
	}
	while(m--)
	{
		std::cin >> op >> x;
		if(op == 1)
		{
			std::cin >> y;
			if(!(tf[x] || tf[y]))
			{
				x = hys::find(x);
				y = hys::find(y);
				if(x != y)
				{
					hys::fa[x] = hys::fa[y] = hys::merge(x, y);
				}
			}
		}
		else
		{
			if(tf[x])
			{
				std::cout << -1 << std::endl;
				continue;
			}
			x = hys::find(x);
			std::cout << hys::t[x].v << std::endl;
			tf[x] = 1;
			hys::t[x].rs = hys::merge(hys::t[x].ls, hys::t[x].rs);
			hys::fa[hys::t[x].ls] = hys::t[x].rs;
			hys::fa[hys::t[x].rs] = hys::t[x].rs;
			hys::t[x].ls = hys::t[x].rs = hys::t[x].dist = 0;
		}
	}
	return 0;
	std::exit(0);
}
2024/1/2 21:05
加载中...