Treap树(并查集 + 树堆)POJ —— 2985 The k-th Largest Group

对应POJ题目:点击打开链接

The k-th Largest Group
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 7863   Accepted: 2528

Description

Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to group some of the cats. To do that, he first offers a number to each of the cat (1, 2, 3, …, n). Then he occasionally combines the group cat i is in and the group cat j is in, thus creating a new group. On top of that, Newman wants to know the size of the k-th biggest group at any time. So, being a friend of Newman, can you help him?

Input

1st line: Two numbers N and M (1 ≤ N, M ≤ 200,000), namely the number of cats and the number of operations.

2nd to (m + 1)-th line: In each line, there is number C specifying the kind of operation Newman wants to do. If C = 0, then there are two numbers i and j (1 ≤ i, jn) following indicating Newman wants to combine the group containing the two cats (in case these two cats are in the same group, just do nothing); If C = 1, then there is only one number k (1 ≤ k ≤ the current number of groups) following indicating Newman wants to know the size of the k-th largest group.

Output

For every operation “1” in the input, output one number per line, specifying the size of the kth largest group.

Sample Input

10 10
0 1 2
1 4
0 3 4
1 2
0 5 6
1 1
0 7 8
1 1
0 9 10
1 1

Sample Output

1
2
2
2
2

Hint

When there are three numbers 2 and 2 and 1, the 2nd largest number is 2 and the 3rd largest number is 1.

 

题意:

有n只猫按1~n编号,初始时各自在各自集合,有m个操作;(0,a, b)表示把编号为a和b的猫所在的集合合并;(1, k)表示询问第k大的集合里有多少只猫(第一大的集合表示猫最多的集合)。

 

思路:

以每个集合猫的个数为key建立树堆,由于有相同元素,所以要多加个count记录一个结点相同元素的个数,对于(0, a, b)操作,利用并查集找到所在集合,如果是相同集合就不做任何事;如果在不同集合,就以各自所在集合猫的个数为key在树堆里查找并删除。接着用合并两个集合后把新集合元素个数插入树堆,插入删除都要记得Push_up;(1, k)操作就直接按size寻找。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <time.h>
using namespace std;
#define MAX_LIMIT (int)(((long)(RAND_MAX+1)/10)*10-1)
#define N 200010
int fa[N];
int Len[N];

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

void InitTime()
{
	static int first = 1;
	if(first){
		srand((unsigned int)time(0));
		first = 0;
	}
}

int Rand(int n) //产生1~2*10^n的随机数
{
	InitTime();
	int i, val = 0, tmp;
	for(i = 0; i < n; i++){
		val *= 10;
		do{
			tmp = rand();
		}while(tmp > MAX_LIMIT);
		val += tmp % 10;
	}
	tmp = rand() % 2 + 1;
	return tmp * (val + 1);
}

struct Node
{
	int len, pri, count, sz;
	Node *l, *r;
};

class TreapTree
{
public:
	void Init(int n)
	{
		rt = NULL;
		int i;
		for(i = 1; i <= n; i++){
			fa[i] = i;
			Len[i] = 1;
			Insert(rt, 1);
		}
	}
	inline void Push_up(Node *T)
	{
		if(NULL == T) return;
		T->sz = (T->l ? T->l->sz : 0) + (T->r ? T->r->sz : 0) + T->count;
	}
	void R_rotate(Node *&x) //没有前驱,旋转需要引用
	{
		Node *y = x->l;
		x->l = y->r;
		y->r = x;
		Push_up(x);
		x = y;
	}
	void L_rotate(Node *&x)
	{
		Node *y = x->r;
		x->r = y->l;
		y->l = x;
		Push_up(x);
		x = y;
	}
	void Insert(Node *&T, int l)
	{
		if(NULL == T){
			T = (Node *)malloc(sizeof(Node));
			T->len = l;
			T->pri = Rand(5);
			T->count = T->sz = 1;
			T->l = T->r = NULL;
		}
		else if(l == T->len) T->count++;
		else if(l > T->len){
			Insert(T->l, l);
			if(T->l->pri < T->pri) R_rotate(T);
		}
		else{
			Insert(T->r, l);
			if(T->r->pri < T->pri) L_rotate(T);
		}
		Push_up(T);
	}
	void Delete(Node *&T, int l)
	{
		if(NULL == T) return;
		else if(l == T->len){
			if(T->count > 1) T->count--;
			else if(NULL == T->l && NULL == T->r){
				free(T);
				T = NULL;
			}
			else if(NULL == T->r){
				Node *tmp = T;
				T = T->l;
				free(tmp);
			}
			else if(NULL == T->l){
				Node *tmp = T;
				T = T->r;
				free(tmp);
			}
			else{
				if(T->l->pri < T->r->pri){
					R_rotate(T); Delete(T->r, l);
				}
				else{
					L_rotate(T); Delete(T->l, l);
				}
			}
		}
		else if(l > T->len) Delete(T->l, l);
		else Delete(T->r, l);
		Push_up(T);
	}
	void Combine(int a, int b)
	{
		a = Find(a);
		b = Find(b);
		if(a == b) return; //a, b在同一个集合
		fa[b] = a;
		Delete(rt, Len[a]);
		Delete(rt, Len[b]);
		Len[a] += Len[b];
		Len[b] = 0;
		Insert(rt, Len[a]);
	}
	void K_th(int x)
	{
		if(NULL == rt) return;
		Node *p;
		p = rt;
		int sum = (p->l ? p->l->sz : 0) + p->count;
		while(sum != x)
		{
			if(sum < x){
				p = p->r;
				x -= sum;
			}
			else{
				if(sum - p->count + 1 <= x) break;
				else p = p->l;
			}
			if(NULL == p) break;
			sum = (p->l ? p->l->sz : 0) + p->count;
		}
		printf("%d\n", p->len);
	}
	void Show()
	{
		InOrder(rt);
		printf("\n");
	}
	void InOrder(Node *T)
	{
		if(NULL == T) return;
		InOrder(T->l);
		printf("%d(%d) ", T->len, T->count);
		InOrder(T->r);
	}
	void Free()
	{
		FreeTree(rt);
	}
	void FreeTree(Node *T)
	{
		if(NULL == T) return;
		FreeTree(T->l);
		FreeTree(T->r);
		free(T);
	}
private:
	Node *rt;
};

TreapTree tre;

int main()
{
	//freopen("in.txt","r",stdin);
	int n, m, op, a, b;
	while(scanf("%d%d", &n, &m) == 2)
	{
		tre.Init(n);
		while(m--)
		{
			scanf("%d", &op);
			if(0 == op){
				scanf("%d%d", &a, &b);
				tre.Combine(a, b);
			}
			else{
				scanf("%d", &a);
				tre.K_th(a);
			}
		}
		tre.Free();
	}
	return 0;
}


 

 

 

 

你可能感兴趣的:(Treap树(并查集 + 树堆)POJ —— 2985 The k-th Largest Group)