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);
}


你可能感兴趣的:(并查集,treap,平衡树,按秩合并,bzoj)