Live2d Test Env

POJ2985 The k-th Largest Group (并查集+treap)

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.

并查集+treap。

开始打算把个数当成第一关键字,id当成第二关键字(weight,rnd),发现处理起来和麻烦,何况一个点可能记录有多个相同数值的点。

就只记录个数。

和上一题有些像,只是多了一个删除函数,一直向下移再删去即可。

不过写了这么几道题,还是对地址符的运用不太理解和熟练。

 

还可以用树状数组或者线段树来解决,以后再试一试。

(到时候线段树套平衡树有得我学了。。。ORZ)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=440010;
const int inf=1e9;
int f[maxn],a[maxn];
int find(int x)
{
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
struct Treap
{
     int ch[maxn][2],size[maxn],cnt[maxn],rnd[maxn],val[maxn],root,Cnt;
     Treap()
     {
            Cnt=1;
            root=0;
            rnd[0]=inf;
     }
     void update(int x)
     {
         size[x]=cnt[x]+size[ch[x][0]]+size[ch[x][1]];
     }
     void insert(int &now,int x)//地址符,别忘记 
     {
            if(now){
                if(val[now]==x) cnt[now]++;
                else {
                   int t=x>val[now];//now可能会变,所以用t。 
                   insert(ch[now][t],x);  //操作有儿子的。 
                   if(rnd[now]>rnd[ch[now][t]]) rotate(now,t);
                } 
            }
            else {//无儿子,不操作。 
                now=++Cnt;
                val[now]=x;
                cnt[now]=1;
                //size[now]=1;后面会更新 
                rnd[now]=rand();
                ch[now][0]=ch[now][1]=0;
            }
            update(now);
     }
     int rotate(int &now,int t)
     {
            int son=ch[now][t];
            ch[now][t]=ch[son][1-t];
            ch[son][1-t]=now;
            update(now);
            update(son);
            now=son;//这里其实不是很理解。 
     }
     void erase(int &now,int k)
     {
        if(val[now]==k){
            if(cnt[now]>1) cnt[now]--;
            else{
                if(ch[now][0]==0&&cnt[ch[now][1]]==0)
                {
                    now=0;
                    return ;
                }
                int t=rnd[ch[now][0]]>rnd[ch[now][1]];
                rotate(now,t);
                erase(now,k);
            }
        }
        else erase(ch[now][val[now]<k],k);
        update(now);
     }
     int query(int now,int k)
     {
          if(size[ch[now][0]]>=k) return query(ch[now][0],k);
          k-=(size[ch[now][0]]+cnt[now]);
          if(k<=0) return val[now];
          return query(ch[now][1],k);
     }
};
Treap treap;
int main()
{
    int n,m,i,k,x,y;
    scanf("%d%d",&n,&m);
    for(i=0;i<=n;i++) f[i]=i,a[i]=1;
    for(i=1;i<=n;i++) treap.insert(treap.root,1);
    for(i=1;i<=m;i++){
        scanf("%d",&k);
        if(!k) {
            scanf("%d%d",&x,&y);
            x=find(x),y=find(y);
            if(x==y) continue;
            f[y]=x;
            treap.erase(treap.root,a[x]);
            treap.erase(treap.root,a[y]);
            a[x]+=a[y];  
            treap.insert(treap.root,a[x]);
            n--;//!
        }
        else {
            scanf("%d",&k);
            printf("%d\n",treap.query(treap.root,n-k+1)); //反着找
        }
    }
    return 0;
}

 

posted @ 2017-11-27 21:04  nimphy  阅读(200)  评论(0编辑  收藏  举报