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

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.

题意:首先给你N只老鼠,M个操作;最开始一只老鼠一个集团

     输入0,输入两个数i,j;使得i老鼠与j老鼠的集团合并;

     输入1,输入一个数K,求第K大的集团有多少只老鼠;

思路:数组的求第K大的话,直接sort(so easy);

   然而这题却是动态的;使用树状数组求第K小;并查集合并集团;

    那个o(logn)求第K小的函数很神奇,自己手动模拟下;代码有注释;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = 0 , ch ;
while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
{
if( ch == EOF ) return 1 << 30 ;
}
res = ch - '0' ;
while( ( ch = getchar() ) >= '0' && ch <= '9' )
res = res * 10 + ( ch - '0' ) ;
return res ;
}
#define maxn 1<<18
int father[maxn];
int flag[maxn];
int tree[maxn],n,m;
int findfa(int x)
{
return father[x]==x ? x: father[x]=findfa(father[x]);
}
int lowbit(int x)//求二进制最小位
{
return x&-x;
}
void update(int x,int change)//更新树状数组
{
while(x<=n)
{
tree[x]+=change;
x+=lowbit(x);
}
}
void add(int x,int y,int &fk)//并查集合并,更新
{
int xx=findfa(x);
int yy=findfa(y);
if(xx!=yy)
{
father[yy]=xx;//少个y超时n遍
update(flag[xx],-1);
update(flag[yy],-1);
update(flag[xx]=flag[xx]+flag[yy],1);
flag[yy]=0;
fk--;
}
}
int k_thsmall(int K)//求第k小
{
int sum=0,i;//初始化
for(i=18;i>=0;i--)
{
if(sum+(1<<i)<=n&&tree[sum+(1<<i)]<K)
{
K-=tree[sum+(1<<i)];
sum+=1<<i;
}
}
return sum+1;
}
int main()
{
int x,y,z,i,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<=n;i++)
father[i]=i;
for(i=0;i<=n;i++)
flag[i]=1;
update(1,n);
int fk=n;
while(m--)
{
scanf("%d",&y);
if(y)
{
scanf("%d",&z);
printf("%d\n",k_thsmall(fk-z+1));//集团的数量需要改变,所以n要变
}
else
{
scanf("%d%d",&z,&t);
add(z,t,fk);
}
}
}
return 0;
}

  

最新文章

  1. linux查看端口占用情况
  2. Session for SSRS Report of Microsoft Dynamics AX
  3. [Machine Learning &amp; Algorithm] 朴素贝叶斯算法(Naive Bayes)
  4. VHDL生成的ngc文件被verilog的工程调用的问题
  5. Linux修改命令提示符(关于环境参量PS1)
  6. 那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)[zz]
  7. echart图表控件配置入门(二)常用图表数据动态绑定
  8. hash --C++
  9. c#之委托总结
  10. 命令行创建Android应用,生成签名,对APK包签名并编译运行
  11. Android 自定义对话框(Dialog)位置,大小
  12. SQL语法集锦一:SQL语句实现表的横向聚合
  13. NGINX结合SHELL统计用户的UV及IP汇总
  14. android 遇到的细节 FAQ
  15. activiti07- Task
  16. JavaScirpt的this指向 apply().call(),bind()个人笔记
  17. why?
  18. 【原创】ucos信号量的操作及原理
  19. logstash配置
  20. WPF应用程序exe接收参数

热门文章

  1. 6*17点阵的Window程序, Java写的。
  2. eclipse Juno Indigo Helios Galileo这几种版本的意思
  3. Perl的基本语法&lt;总结&gt; (转载)
  4. Python精神
  5. require()与 require_once()、 include与include_once()
  6. jquery.rotate.js库中的rotate函数怎么用。
  7. Window下memcached安装与测试步骤
  8. Xcode中设置按钮在十分钟之内禁用
  9. 修改tomcat的logo
  10. SQL Server 索引和表体系结构(二)