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


你可能感兴趣的:(Poj 2985 树状数组求第k大)