BZOJ 2733 HNOI 2012 永无乡 平衡树启示式合并

简介:

题目大意:有一些岛屿,一開始由一些无向边连接。

后来也有不断的无向边增加,每个岛屿有个一独一无二的重要度,问随意时刻的与一个岛屿联通的全部岛中重要度第k大的岛的编号是什么。


思路:首先连通性一定要用并查集维护。然后就是联通快内的第k大问题,显然是平衡树。可是并查集的合并怎么搞?能够考虑按秩合并,这种话就保证每次在平衡树中处理的元素尽量的少,就能够水过这个题了。

注意一下输出-1的推断。


CODE:

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define SIZE(a) (a == NULL ? 0:a->size)
using namespace std;

map<int,int> G;

struct Complex{
	int val,random,size,cnt;
	Complex *son[2];
	
	Complex(int _) {
		val = _;
		random = rand();
		size = cnt = 1;
		son[0] = son[1] = NULL;
	}
	int Compare(int x) {
		if(x == val)	return -1;
		return x > val;
	}
	void Maintain() {
		size = cnt;
		if(son[0] != NULL)	size += son[0]->size;
		if(son[1] != NULL)	size += son[1]->size;
	}
};

int points,edges,asks;
int src[MAX];
int father[MAX],cnt[MAX];

Complex *tree[MAX];

char c[10];

int Find(int x);

inline void Rotate(Complex *&a,bool dir);
void Insert(Complex *&a,int x);
void Delete(Complex *&a,int x);
int Kth(Complex *a,int k);

int main()
{
	cin >> points >> edges;
	for(int i = 1;i <= points; ++i) {
		father[i] = i,cnt[i] = 1;
		scanf("%d\n",&src[i]);
		G[src[i]] = i;
	}
	for(int x,y,i = 1;i <= edges; ++i) {
		scanf("%d%d",&x,&y);
		int fx = Find(x);
		int fy = Find(y);
		if(fx != fy) {
			father[fy] = fx;
			cnt[fx] += cnt[fy];
		}
	}
	for(int i = 1;i <= points; ++i) {
		int fx = Find(i);
		Insert(tree[fx],src[i]);
	}
	cin >> asks;
	for(int x,y,i = 1;i <= asks; ++i) {
		scanf("%s%d%d",c,&x,&y);
		if(c[0] == 'Q') {
			int fx = Find(x);
			if(y > cnt[fx])	puts("-1");
			else	printf("%d\n",G[Kth(tree[fx],y)]);
		}
		else {
			int fx = Find(x);
			int fy = Find(y);
			if(fx != fy) {
				if(cnt[fy] > cnt[fx])	swap(fx,fy);
				father[fy] = fx;
				cnt[fx] += cnt[fy];
				for(int j = 1;j <= cnt[fy]; ++j) {
					int temp = Kth(tree[fy],1);
					Delete(tree[fy],temp);
					Insert(tree[fx],temp);
				}
			}
		}
	}
	return 0;
}

int Find(int x)
{
	if(father[x] == x)	return x;
	return father[x] = Find(father[x]);
}

inline void Rotate(Complex *&a,bool dir)
{
	Complex *k = a->son[!dir];
	a->son[!dir] = k->son[dir];
	k->son[dir] = a;
	a->Maintain(),k->Maintain();
	a = k;
}

inline void Insert(Complex *&a,int x)
{
	if(a == NULL) {
		a = new Complex(x);
		return ;
	}
	int dir = a->Compare(x);
	if(dir == -1)
		a->cnt++;
	else {
		Insert(a->son[dir],x);
		if(a->son[dir]->random > a->random)
			Rotate(a,!dir);
	}
	a->Maintain();
}

void Delete(Complex *&a,int x)
{
	int dir = a->Compare(x);
	if(dir != -1)
		Delete(a->son[dir],x);
	else {
		if(a->cnt > 1)
			--a->cnt;
		else {
			if(a->son[0] == NULL)	a = a->son[1];
			else if(a->son[1] == NULL)	a = a->son[0];
			else {
				bool _dir = a->son[0]->random > a->son[1]->random;
				Rotate(a,_dir);
				Delete(a->son[_dir],x);
			}
		}
	}
	if(a != NULL)	a->Maintain();
}

int Kth(Complex *a,int k)
{
	if(k <= SIZE(a->son[0]))	return Kth(a->son[0],k);
	k -= SIZE(a->son[0]);
	if(k <= a->cnt)	return a->val;
	return Kth(a->son[1],k - a->cnt);
}




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5246546.html,如需转载请自行联系原作者



相关文章
|
6天前
|
人工智能 运维 安全
|
3天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
5天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
499 14
|
11天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
868 109
|
4天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。