POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

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

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.

思路

关于树状数组求第K大的数的知识

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 200005;
int N,root[maxn],a[maxn],c[maxn];
 
int find(int x)
{
    if (root[x] != x)   root[x] = find(root[x]);
    return root[x];
}
 
int find_kth(int x) //查找第k小的元素
{
    int ans = 0,cnt = 0;
    for (int i = 20;i >= 0;i--)   //这里的20适当的取值,与MAX_VAL有关,一般取lg(MAX_VAL)
    {
        ans += (1 << i);
        if (ans > N || cnt + c[ans] >= x)
            ans -= (1 << i);
        else
            cnt += c[ans];
    }
    return ans + 1;
}
 
void upd(int i,int v)
{
    while (i <= N)
    {
        c[i] += v;
        i += i & -i;
    }
}
 
int main()
{
    int M;
    while (~scanf("%d%d",&N,&M))
    {
        int tot = N;
        memset(c,0,sizeof(c));
        for (int i = 1;i <= N;i++)   root[i] = i,a[i] = 1; //a[i]表示编号为i的Group大小
        upd(1,N);                //初始Group大小为1的有N个
        while (M--)
        {
            int opt,x,y;
            scanf("%d",&opt);
            if (opt == 0)
            {
                scanf("%d%d",&x,&y);
                x = find(x);
                y = find(y);
                if (x == y) continue;
                upd(a[x],-1);
                upd(a[y],-1);       //x与y合并,则编号为x的Group与编号为y的Group大小减1
                upd(a[x] + a[y],1); //大小为a[x]+a[y]的Group的大小增1
                root[y] = x;        //y的祖先节点为x;
                a[x] += a[y];       //编号为x的Group大小增加a[y]
                tot--;              //并查集合并,则总元素减少1
            }
            else
            {
                scanf("%d",&x);
                printf("%d\n",find_kth(x));
            }
        }
    }
    return 0;
}
posted @   zxzhang  阅读(304)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示