POJ 2985 the k-th largest Group (tree array and look up/find the number of K large)

Source: Internet
Author: User

Transmission Doorthe k-th largest Group
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 8690 Accepted: 2847


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 does, he first offers a number to each of the cat (1, 2, 3, ..., n). Then he occasionally combines the group cat I was in and the group Cat J was in, thus creating a new group . On top of this, Newman wants to know the size of the K--th biggest group at any time. So, being a friend of Newman, canI help hi m?


    • 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 All line, there is number  C & Nbsp;specifying the kind of operation Newman wants to do. if  C  = 0, then there is and numbers  i  and  J   (1≤  i ,   J  ≤  n ) following indicating Newman wants to combine the group containing the C ATS (in case these, cats is in the same group, just does nothing); if  C  = 1, then there are only one number  k   (1≤  k  ≤the current N Umber of groups) following indicating Newman wants to know the size of the  K -th largest group.


For every operation "1" with 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







When there was three numbers 2 and 2 and 1, the 2nd largest number is 2 and the 3rd largest number is 1.

#include <cstdio> #include <algorithm> #include <cstring>using namespace std;const int maxn = 200005; int n,root[maxn],a[maxn],c[maxn];int Find (int x) {if (root[x]! = x) root[x] = find (root[x]); return root[x];} int find_kth (i NT x)//Find k small element {int ans = 0,CNT = 0;for (int i = 20;i >= 0;i--)//20 appropriate value here, related to Max_val, general take LG (Max_val) {ans + = (1 &l t;< i); if (Ans > N | | cnt + C[ans] >= x) ans-= (1 << i); elsecnt + = C[ans]; }return ans + 1;} void upd (int i,int v) {while (I <= N) {C[i] + = V;i + i &-I;}} int main () {int m;while (~scanf ("%d%d", &n,&m)) {int tot = N;memset (c,0,sizeof (c)); for (int i = 1;i <= n;i++) root[ I] = i,a[i] = 1;                A[i] represents the group size UPD (1,n) numbered I; The initial group size is 1 with n while (m--) {int opt,x,y;scanf ("%d", &opt), if (opt = = 0) {scanf ("%d%d", &x,&y); x = Find (x); y       = Find (Y), if (x = = y) continue;upd (a[x],-1); upd (a[y],-1); X is merged with Y, the group with number x is reduced by 1UPD (A[x] + a[y],1) with the group size number y;        Size of a[x]+a[y] group size increased by 1 root[y] = x;Y's ancestor node is x;a[x] + = A[y];              The group size with number x increases a[y]tot--; Combined with a collection merge, the total element is reduced by 1}else{scanf ("%d", &x);p rintf ("%d\n", find_kth (x));}}} return 0;}


POJ 2985 the k-th largest Group (tree array and look up/find the number of K large)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.