POJ 2985 The k-th Largest Group

新文探讨了猫主人如何通过编号与操作对大量猫咪进行分组,并实时获取特定分组大小的方法,运用并查集与堆实现高效管理。


The  k-th Largest Group
Time Limit: 2000MSMemory Limit: 131072K
Total Submissions: 6776Accepted: 2179

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.

Source



 Treap+并查集模板题




#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

struct Disjoin
{
    vector<int>father,ran;
    Disjoin(int n):father(n),ran(n)
    {
        for(int i=0;i<n;i++)
        {
            father=i;
            ran=1;
        }
    }

    int Found(int v)
    {
        return father[v]=father[v]==v?v:Found(father[v]);
    }

    int Merge(int x,int y)
    {
        int a=Found(x),b=Found(y);
        if(a==b) return 0;
        if(ran[a]<ran)
        {
            father=a;
            ran[a]+=ran;
        }
        else
        {
            father[a]=b;
            ran+=ran[a];
        }
        return 1;
    }

    int getRan(int x)
    {
        int a=Found(x);
        return ran[a];
    }
};

const int maxNode=444444,INF=0x3f3f3f3f;
int root,treapCnt,key[maxNode],priority[maxNode],childs[maxNode][2],cnt[maxNode],ssize[maxNode];
struct Treap
{

    Treap()
    {
        root=0; treapCnt=1;
        priority[0]=INF;
        ssize[0]=0;
    }

    void update(int x)
    {
        ssize[x]=ssize[childs[x][0]]+cnt[x]+ssize[childs[x][1]];
    }

    void rotate(int&x,int t)
    {
        int y=childs[x][t];
        childs[x][t]=childs[y][1-t];
        childs[y][1-t]=x;
        update(x);  update(y);
        x=y;
    }

    void _insert(int&x,int k)
    {
        if(x)
        {
            if(key[x]==k)
            {
                cnt[x]++;
            }else
            {
                int t=key[x]<k;
                _insert(childs[x][t],k);
                if(priority[childs[x][t]]<priority[x])
                {
                    rotate(x,t);
                }
            }
        }
        else
        {
            x=treapCnt++;
            key[x]=k;
            cnt[x]=1;
            priority[x]=rand();
            childs[x][0]=childs[x][1]=0;
        }
        update(x);
    }

    void _erase(int& x,int k)
    {
        if(key[x]==k)
        {
            if(cnt[x]>1)
            {
                cnt[x]--;
            }
            else
            {
                if(childs[x][0]==0&&childs[x][1]==0)
                {
                    x=0;
                    return ;
                }
                int t=priority[childs[x][0]]>priority[childs[x][1]];
                rotate(x,t);
                _erase(x,k);
            }
        }
        else
        {
            _erase(childs[x][key[x]<k],k);
        }
        update(x);
    }

    int _getKth(int& x,int k)
    {
        if(k<=ssize[childs[x][0]])
        {
            return _getKth(childs[x][0],k);
        }
        k-=ssize[childs[x][0]]+cnt[x];
        if(k<=0)
        {
            return key[x];
        }
        return _getKth(childs[x][1],k);
    }

    void insert(int k)
    {
        _insert(root,k);
    }
    void erase(int k)
    {
        _erase(root,k);
    }
    int getKth(int k)
    {
        return _getKth(root,k);
    }

}T;

int main()
{
    int N,M,c,a,b,cnt;
    scanf("%d%d",&N,&M);
    Disjoin U(N);
    for(int i=0;i<N;i++)
    {
        T.insert(1);
    }
    cnt=N;
    while(M--)
    {
        scanf("%d",&c);
        if(c==0)
        {
            scanf("%d%d",&a,&b);
            int sa=U.getRan(a-1),sb=U.getRan(b-1);
            if(U.Merge(a-1,b-1))
            {
                T.erase(sa);T.erase(sb);
                T.insert(sa+sb);
                cnt--;
            }
        }
        else if(c==1)
        {
            scanf("%d",&a);
            printf("%d\n",T.getKth(cnt-(a-1)));
        }
    }

    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Codeblocks )

转载于:https://www.cnblogs.com/CKboss/p/3350900.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值