Poj 2985 树状数组求第k大



链接:戳这里


The k-th Largest Group
Time Limit: 2000MS Memory Limit: 131072K
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, j ≤ 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大的组的大小


思路:树状数组维护两个值,一个是猫的集合大小num[i],其中i为该集合的父亲,sum[i]表示树状数组管辖的区间内的和,对应的就是猫的集合从小到大的和 一次被sum[i]记录下来了


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 200000
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int fa[2000100];
int find(int x){
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
int num[200100];  /// 猫的集合大小为num[i]  i是父亲
int sum[200100];  /// 树状数组  管辖  区间内的总和
int lowbit(int x){
    return x&(-x);
}
void Add(int x,int v){
    while(x<=n){
        sum[x]+=v;
        x+=lowbit(x);
    }
}
int k_num(int k){
    int ans=0,w=0;
    for(int i=18;i>=0;i--){
        w+=(1<<i);
        if(w>n || k<=sum[w]) w-=(1<<i);
        else k-=sum[w];
    }
    return w+1;
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=1;i<=n;i++) fa[i]=i,num[i]=1;
        Add(1,n);
        int tot=n;
        for(int i=1;i<=m;i++){
            int x,y,z,k;
            scanf("%d",&x);
            if(x==0){
                scanf("%d%d",&y,&z);
                int xx=find(y);
                int yy=find(z);
                if(xx!=yy){
                    fa[xx]=yy;
                    Add(num[xx],-1);
                    Add(num[yy],-1);
                    num[yy]=num[xx]+num[yy];
                    Add(num[yy],1);
                    tot--;
                }
            } else {
                scanf("%d",&k);
                printf("%d\n",k_num(tot-k+1));
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要对给定的n个进行排序,并且输出每个在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的据结构。 根据引用中的描述,我们可以通过遍历组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的的个。这个个就是它在排序后的相对位置。 代码中的query函用来前缀和,add函用来更新树状数组。在主函中,我们从后往前遍历组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans组中。 最后,我们按顺序输出ans组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要的排序功能,其中query函用来前缀和,add函用来更新树状数组。在主函中,我们从后往前遍历组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans组中。最后,我们按顺序输出ans组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值