POJ 2985:The k-th Largest Group 树状数组求第K小的元素

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

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 ≤ NM ≤ 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 ≤ ij ≤ n) 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.

题意是一个人养了一堆猫,每个猫有自己的一个编号,他喜欢把猫分成组,并且操作为0的时候意味着将两只猫代表的组合并到一起。操作为0的时候询问第k大的组有多少只猫。

并查集+树状数组。记录一些这道题是学到了树状数组logn内求第k小的元素。

二分代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>;
#include <set>
#include <map>
using namespace std;

#define INF 0x3fffffff
typedef long long ll;

const ll mod = 1e9 + 7;
const int maxn = 200005;

int n, m;
int fa[maxn], num[maxn], ans[maxn];

int lowbit(int x)
{
	return (x&(-x));
}

int find(int x)
{
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
	fa[x] = y;
}

void add(int x, int y)
{
	while (x <= n)
	{
		ans[x] += y;
		x += lowbit(x);
	}
}

int getsum(int x)
{
	int res = 0;
	while (x > 0)
	{
		res += ans[x];
		x -= lowbit(x);
	}
	return res;
}

void solve()
{
	int i, u, v, k, oper, group_n;
	int le, ri, mid;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i++)
	{
		fa[i] = i;
		num[i] = 1;
	}
	add(1, n);
	group_n = n;
	for (i = 1; i <= m; i++)
	{
		scanf("%d", &oper);
		if (oper == 1)
		{
			scanf("%d", &k);
			k = group_n - k + 1;
			le = 1; ri = n;

			while (le < ri)
			{
				mid = (le + ri) >> 1;
				if (getsum(mid) >= k)
					ri = mid;
				else
					le = mid + 1;
			}
			printf("%d\n", ri);
		}
		else
		{
			scanf("%d%d", &u, &v);
			u = find(u);
			v = find(v);
			if (u == v)continue;
			merge(u, v);
			add(num[u], -1);
			add(num[v], -1);
			
			num[v] = num[u] + num[v];
			add(num[v], 1);
			group_n--;
		}
	}
}

int main()
{
	//freopen("i.txt","r",stdin);
	//freopen("o.txt","w",stdout);
	solve();
	//system("pause");
	return 0;
}

find_kth代码,少了100ms:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>;
#include <set>
#include <map>
using namespace std;

#define INF 0x3fffffff
typedef long long ll;

const ll mod = 1e9 + 7;
const int maxn = 200005;

int n, m;
int fa[maxn], num[maxn], ans[maxn];

int lowbit(int x)
{
	return (x&(-x));
}

int find(int x)
{
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}

void merge(int x, int y)
{
	fa[x] = y;
}

void add(int x, int y)
{
	while (x <= n)
	{
		ans[x] += y;
		x += lowbit(x);
	}
}

int getsum(int x)
{
	int res = 0;
	while (x > 0)
	{
		res += ans[x];
		x -= lowbit(x);
	}
	return res;
}

int find_kth(int k)
{
	int res = 0, cnt = 0, i;
	for (i = 20; i >= 0; i--)
	{
		res += (1 << i);
		if (res >= n || cnt + ans[res] >= k)
		{
			res -= (1 << i);
		}
		else
		{
			cnt += ans[res];
		}
	}
	return res + 1;
}

void solve()
{
	int i, u, v, k, oper, group_n;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i++)
	{
		fa[i] = i;
		num[i] = 1;
	}
	add(1, n);
	group_n = n;
	for (i = 1; i <= m; i++)
	{
		scanf("%d", &oper);
		if (oper == 1)
		{
			scanf("%d", &k);
			k = group_n - k + 1;

			printf("%d\n", find_kth(k));
		}
		else
		{
			scanf("%d%d", &u, &v);
			u = find(u);
			v = find(v);
			if (u == v)continue;
			merge(u, v);
			add(num[u], -1);
			add(num[v], -1);
			
			num[v] = num[u] + num[v];
			add(num[v], 1);
			group_n--;
		}
	}
}

int main()
{
	//freopen("i.txt","r",stdin);
	//freopen("o.txt","w",stdout);
	solve();
	//system("pause");
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值