poj 2985 The k-th Largest Group 树状数组求第K大
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 ≤ 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.
题意:首先给你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;
}
poj 2985 The k-th Largest Group 树状数组求第K大的更多相关文章
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- *HDU2852 树状数组(求第K小的数)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 树状数组求第K大(From CLJ)
; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
随机推荐
- struct termios结构体【转】
本文转载自:http://blog.csdn.net/vevenlcf/article/details/51096122 一.数据成员 termios 函数族提供了一个常规的终端接口,用于控制非同步通 ...
- iOS 学习笔记 七 (2015.03.29)code snippet操作
1.code snippet 备份路径:~/Library/Developer/Xcode/UserData/CodeSnippets/
- android 之 Crash信息的持久化处理
需求: 持久化运行时异常的信息 1.CrashHandler.java import android.content.Context; import android.content.pm.Packag ...
- Linux下创建与解压tar, tar.gz和tar.bz2文件及压缩率对比 | 沉思小屋
刚 在qq群里面一位仁兄问到文件压缩的命令,平时工作中大多用解压缩命令,要是遇到压缩就现查(这不是一个好习惯),于是整理下Linux下创建与解压 zip.tar.tar.gz和tar.bz2文件及他们 ...
- Oracle 单行函数
一.什么是函数 任何东西,只要它能接收输入,对输入进行加工并产生输出,它就可以被称为函数. 二.单行函数简介 单行函数只对表中的一行数据进行操作,并且对每一行数据只产生一个输出结果.单行函数可以接受一 ...
- C#:DataTable内容转换为String(XML)
//DataTable转String方法 public static String DataTable2String(DataTable dt) { string strXML = "< ...
- jquery ui 常用(二)(对话框 | 旋转器 | 工具提示框(表单) | 特效(百叶窗) )
一.添加信息的对话框 http://www.w3cschool.cc/try/tryit.php?filename=jqueryui-example-dialog-modal-form. 模态表单 ...
- [Unity3D]MonoBehaviour函数介绍
原文地址: http://www.cocos2dev.com/?p=486 Unity中的脚本都是继承自MonoBehaviour. 一.基础函数: 创建脚本就默认的update.start方法:(这 ...
- ACM题目————士兵杀敌(四)
描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战(编号相近的 人经常在一块,相互之间比较熟悉),最终他们获得的军功,也将会平分到每个人身上, ...
- 在lua的string库和正则表达式
一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作 ...