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 ",T.getKth(cnt-(a-1)));
        }
    }

    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Codeblocks )
原文地址:https://www.cnblogs.com/CKboss/p/3350900.html