• POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]


    The k-th Largest Group
    Time Limit: 2000MS   Memory Limit: 131072K
    Total Submissions: 8807   Accepted: 2875

    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.

    Source


    并查集维护连通分量大小,树状数组求cc中第k大值

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    const int N=2e5+5;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,m,op,x,y,k;
    int fa[N],size[N],tot=0;
    inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    
    int c[N];
    inline int lowbit(int x){return x&-x;}
    inline void add(int p,int v){
        for(;p<=n;p+=lowbit(p)) c[p]+=v;
    }
    inline int sum(int p){
        int res=0;
        for(;p>0;p-=lowbit(p)) res+=c[p];
        return res;
    }
    inline int kth(int k){
        int x=0,cnt=0;
        for(int i=16;i>=0;i--){
            x+=(1<<i);
            if(x>=n||cnt+c[x]>=k) x-=(1<<i);
            else cnt+=c[x];
        }
        return x+1;
    }
    
    int main(){
        n=read();m=read();
        for(int i=1;i<=n;i++) fa[i]=i,size[i]=1,tot++;
        add(1,n);
        for(int i=1;i<=m;i++){
            op=read();
            if(!op){
                x=read();y=read();
                int f1=find(x),f2=find(y);
                if(f1!=f2){
                    fa[f1]=f2;
                    add(size[f1],-1);
                    add(size[f2],-1);
                    size[f2]+=size[f1];
                    add(size[f2],1);
                    tot--;
                }
            //    printf("%d %d %d %d
    ",f1,f2,size[f1],size[f2]);
            }else{
                k=tot-read()+1;//printf("k %d
    ",k);
                printf("%d
    ",kth(k));
            }
        }
    }

    当然treap也可以 注意是第k大

    //
    //  main.cpp
    //  poj2985_treap
    //
    //  Created by Candy on 27/11/2016.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    #define lc t[x].l
    #define rc t[x].r
    const int N=2e5+5;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,m,op,x,y,k;
    int fa[N],size[N];
    inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    struct node{
        int l,r,v,w,rnd,size;
    }t[N];
    int cnt,root;
    inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
    inline void rturn(int &x){
        int c=lc;lc=t[c].r;t[c].r=x;
        t[c].size=t[x].size;update(x);x=c;
    }
    inline void lturn(int &x){
        int c=rc;rc=t[c].l;t[c].l=x;
        t[c].size=t[x].size;update(x);x=c;
    }
    void ins(int &x,int v){//printf("ins %d %d
    ",x,v);
        if(x==0){
            cnt++;x=cnt;
            t[cnt].l=t[cnt].r=0;t[cnt].w=t[cnt].size=1;
            t[cnt].v=v;t[cnt].rnd=rand();
        }else{
            t[x].size++;
            if(t[x].v==v) t[x].w++;
            else if(v<t[x].v){
                ins(lc,v);
                if(t[lc].rnd<t[x].rnd) rturn(x);
            }else{
                ins(rc,v);
                if(t[rc].rnd<t[x].rnd) lturn(x);
            }
        }
    }
    void del(int &x,int v){
        if(x==0) return;
        if(t[x].v==v){
            if(t[x].w>1){t[x].w--;t[x].size--;return;}
            if(lc*rc==0) x=lc+rc;
            else if(t[lc].rnd<t[rc].rnd) rturn(x),del(x,v);
            else lturn(x),del(x,v);
        }else{
            t[x].size--;
            if(v<t[x].v) del(lc,v);
            else del(rc,v);
        }
    }
    //int kth(int x,int k){
    //    if(x==0)return 0;
    //    if(k<=t[lc].size) return kth(lc,k);
    //    else if(k>t[lc].size+t[x].w) return kth(rc,k-t[lc].size-t[x].w);
    //    else return t[x].v;
    //}
    int kth(int x,int k){
        if(x==0) return 0;
        if(k<=t[rc].size) return kth(rc,k);
        else if(k>t[rc].size+t[x].w) return kth(lc,k-t[rc].size-t[x].w);
        else return t[x].v;
    }
    int main(){
        n=read();m=read();
        for(int i=1;i<=n;i++) fa[i]=i,size[i]=1;
        while(m--){
            op=read();
            if(!op){
                x=read();y=read();
                int f1=find(x),f2=find(y);
                if(f1!=f2){
                    fa[f1]=f2;
                    if(size[f1]!=1) del(root,size[f1]);
                    if(size[f2]!=1) del(root,size[f2]);
                    size[f2]+=size[f1];
                    ins(root,size[f2]);
                }
            }else{
                k=read();//printf("kth %d %d
    ",k,t[root].size);
                if(k>t[root].size) puts("1");
                else printf("%d
    ",kth(root,k));
            }
        }
    }
  • 相关阅读:
    JAVA 使用JCO3调用SAP RFC函数
    css 子元素margintop影响了父元素
    使用nginx反代nacos报错 badrequest 400
    windows docker desktop配置国内镜像仓库
    进入docker容器命令行
    SAP AUFM 针对订单的货物移动
    nginx docker官方镜像使用自定义启动命令启动失败
    NACOS DOCKER 官方镜像启动失败 No Datasource set
    HighCharts 嵌套仪表盘
    华为云容器引擎 单实例模式部署 NACOS DOCKER
  • 原文地址:https://www.cnblogs.com/candy99/p/6067401.html
Copyright © 2020-2023  润新知