poj2985The k-th Largest Group

链接:http://poj.org/problem?id=2985

题意:初始给定n个元素个数为1的集合,然后给定m个操作,有两种操作:C=0时输入a,b表示将a,b所在的集合合并成一个集合,C=1时输入k表示询问当前所有集合中集合大小排序中的第k大的集合的size。

分析:集合合并直接用并查集维护就可以了,找第k大的话用树状数组找第k大就好了

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=200010;
const int MAX=151;
const int mod=100000000;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
int n,f[N],g[N],p[N];
void add(int x,int y) {
    for (;x<=n;x+=x&(-x)) p[x]+=y;
}
int sum(int x) {
    int ret=0;
    for (;x;x-=x&(-x)) ret+=p[x];
    return ret;
}
int ask_k(int k) {
    int i,w=0;
    for (i=20;i>=0;i--)
    if (w+(1<<i)<=n&&p[w+(1<<i)]<k) {
        k-=p[w+(1<<i)];w+=1<<i;
    }
    return w+1;
}
int find_f(int x) {
    return f[x]==x ? x:f[x]=find_f(f[x]);
}
int main()
{
    int a,b,c,k,i,m,tot;
    scanf("%d%d", &n, &m);
    memset(p,0,sizeof(p));
    for (i=1;i<=n;i++) f[i]=i,g[i]=1;
    add(1,n);tot=n;
    while (m--) {
        scanf("%d", &c);
        if (!c) {
            scanf("%d%d", &a, &b);
            int fa=find_f(a),fb=find_f(b);
            if (fa!=fb) {
                tot--;add(g[fa],-1);add(g[fb],-1);
                f[fa]=fb;g[fb]+=g[fa];add(g[fb],1);
            }
        } else {
            scanf("%d", &k);
            printf("%d\n", ask_k(tot-k+1));
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值