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

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.

思路

关于树状数组求第K大的数的知识

#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(int x) //查找第k小的元素
{
	int ans = 0,cnt = 0;
	for (int i = 20;i >= 0;i--)   //这里的20适当的取值,与MAX_VAL有关,一般取lg(MAX_VAL)
	{
		ans += (1 << i);
		if (ans > N || cnt + c[ans] >= x)
			ans -= (1 << i);
		else
			cnt += 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]表示编号为i的Group大小
		upd(1,N);                //初始Group大小为1的有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与y合并,则编号为x的Group与编号为y的Group大小减1
				upd(a[x] + a[y],1); //大小为a[x]+a[y]的Group的大小增1
				root[y] = x;        //y的祖先节点为x;
				a[x] += a[y];       //编号为x的Group大小增加a[y]
				tot--;              //并查集合并,则总元素减少1
			}
			else
			{
				scanf("%d",&x);
				printf("%d\n",find_kth(x));
			}
		}
	}
	return 0;
}

POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)的更多相关文章

  1. [BZOJ3038]上帝造题的七分钟2 树状数组+并查集

    考试的时候用了两个树状数组去优化,暴力修改,树状数组维护修改后区间差值还有最终求和,最后骗了40分.. 这道题有好多种做法,求和好说,最主要的是开方.这道题过的关键就是掌握一点:在数据范围内,最多开方 ...

  2. poj 2985 The k-th Largest Group 树状数组求第K大

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

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

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

  4. luogu 4145 花神游历各国 线段树/树状数组+并查集

    此题一看便是RMQ问题,但是由于开平方的特殊操作,tag操作失效 此时发现特性:sqrt最多执行6此便使值到达1/0,此时可以剪枝不进行该操作,利用并查集到达特性找根,根代表还可以进行操作的点,再利用 ...

  5. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  6. CodeVS2492 上帝造题的七分钟2(树状数组+并查集)

    传送门 树状数组模板题.注意优化,假设某个数的值已经是1了的话.那么我们以后就不用对他进行操作了,这个能够用并查集实现. 这道题还有个坑的地方,给出查询区间端点的a,b,有可能a>b. #inc ...

  7. BZOJ 3211 花神游历各国 (树状数组+并查集)

    题解:首先,单点修改求区间和可以用树状数组实现,因为开平方很耗时间,所以在这个方面可以优化,我们知道,开平方开几次之后数字就会等于1 ,所以,用数组记录下一个应该开的数,每次直接跳到下一个不是1的数字 ...

  8. BZOJ3211 花神游历各国 【树状数组 + 并查集】

    题目 输入格式 输出格式 每次x=1时,每行一个整数,表示这次旅行的开心度 输入样例 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4 输出样例 101 11 1 ...

  9. [BZOJ3211]花神游历各国&&[BZOJ3038] 上帝造题的七分钟2 树状数组+并查集

    3211: 花神游历各国 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 4057  Solved: 1480[Submit][Status][Discu ...

随机推荐

  1. Webwork 学习之路【05】请求跳转前 xwork.xml 的读取

    个人理解 WebWork 与 Struts2 都是将xml配置文件作为 Controler 跳转的基本依据,WebWork 跳转 Action 前 xml 文件的读取依赖 xwork-1.0.jar, ...

  2. lecture1-NN的简介

    这是DL的发明人Hinton在多伦多大学的2013年冬季教授de课程,并将视频分享到coursera网站上.其中不但有视频,也有课件,但是Hinton主页上还有他上课的课后问题,Hinton告诉学生这 ...

  3. 你真的熟悉background吗?

    一两个月没更新博客了,因为放假刚在深圳找了实习,一直都比较忙碌,不过我觉得再忙,还是需要时间去沉淀一些东西,工作的时候别人看到的只是你有没有实现最终的结果,但自己是否思考,是否去总结,决定着你工作是否 ...

  4. jquery图片轮播效果(unslider)

    今天做网站(住建局网站)需要用到图片轮播,刚开始想借鉴DTCMS上的,查看CSS与页面代码,呵呵,不复杂,直接复制过来,结果调整半天,页面还是各种乱,没办法,网上找一个吧,于是找到了今天要说的这货un ...

  5. js的几种排序

    转载:http://www.jb51.net/article/81520.htm 一.冒泡排序 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  6. android之fragment的使用

    android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...

  7. javascript 连等赋值问题(这是从SegmentFault转过来的一个问题)

    var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);// - ...

  8. datePiker弹出框被其他div遮挡

    最近在做项目的时候,datePiker弹出框被下面的div给遮挡住了,以前也碰到过这样类似的问题,之前直接在style中添加"z-index:1000".但是现在使用angular ...

  9. jq实现登陆页面的拖拽功能

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...

  10. 开发错误记录11:git报错 fatal:open /dev/null or dup failed: No such file or directory

    今天在自己的的电脑上装了git,没成想运行报错: 重装了几次git ,都不行,电脑上没有装github桌面版; 后来在网上查到了方法,反映这是系统的问题: null是比较特殊的系统文件,它实际上是为操 ...