POJ2985 The k-th Largest Group (并查集+treap)
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.
并查集+treap。
开始打算把个数当成第一关键字,id当成第二关键字(weight,rnd),发现处理起来和麻烦,何况一个点可能记录有多个相同数值的点。
就只记录个数。
和上一题有些像,只是多了一个删除函数,一直向下移再删去即可。
不过写了这么几道题,还是对地址符的运用不太理解和熟练。
还可以用树状数组或者线段树来解决,以后再试一试。
(到时候线段树套平衡树有得我学了。。。ORZ)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=;
const int inf=1e9;
int f[maxn],a[maxn];
int find(int x)
{
if(f[x]!=x) f[x]=find(f[x]);
return f[x];
}
struct Treap
{
int ch[maxn][],size[maxn],cnt[maxn],rnd[maxn],val[maxn],root,Cnt;
Treap()
{
Cnt=;
root=;
rnd[]=inf;
}
void update(int x)
{
size[x]=cnt[x]+size[ch[x][]]+size[ch[x][]];
}
void insert(int &now,int x)//地址符,别忘记
{
if(now){
if(val[now]==x) cnt[now]++;
else {
int t=x>val[now];//now可能会变,所以用t。
insert(ch[now][t],x); //操作有儿子的。
if(rnd[now]>rnd[ch[now][t]]) rotate(now,t);
}
}
else {//无儿子,不操作。
now=++Cnt;
val[now]=x;
cnt[now]=;
//size[now]=1;后面会更新
rnd[now]=rand();
ch[now][]=ch[now][]=;
}
update(now);
}
int rotate(int &now,int t)
{
int son=ch[now][t];
ch[now][t]=ch[son][-t];
ch[son][-t]=now;
update(now);
update(son);
now=son;//这里其实不是很理解。
}
void erase(int &now,int k)
{
if(val[now]==k){
if(cnt[now]>) cnt[now]--;
else{
if(ch[now][]==&&cnt[ch[now][]]==)
{
now=;
return ;
}
int t=rnd[ch[now][]]>rnd[ch[now][]];
rotate(now,t);
erase(now,k);
}
}
else erase(ch[now][val[now]<k],k);
update(now);
}
int query(int now,int k)
{
if(size[ch[now][]]>=k) return query(ch[now][],k);
k-=(size[ch[now][]]+cnt[now]);
if(k<=) return val[now];
return query(ch[now][],k);
}
};
Treap treap;
int main()
{
int n,m,i,k,x,y;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) f[i]=i,a[i]=;
for(i=;i<=n;i++) treap.insert(treap.root,);
for(i=;i<=m;i++){
scanf("%d",&k);
if(!k) {
scanf("%d%d",&x,&y);
x=find(x),y=find(y);
if(x==y) continue;
f[y]=x;
treap.erase(treap.root,a[x]);
treap.erase(treap.root,a[y]);
a[x]+=a[y];
treap.insert(treap.root,a[x]);
n--;//!
}
else {
scanf("%d",&k);
printf("%d\n",treap.query(treap.root,n-k+)); //反着找
}
}
return ;
}
POJ2985 The k-th Largest Group (并查集+treap)的更多相关文章
- 51 nod 1456 小K的技术(强连通 + 并查集)
1456 小K的技术 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 苏塞克王国是世界上创新技术的领先国家,在王国中有n个城市 ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- [poj-2985]The k-th Largest Group_Treap+并查集
The k-th Largest Group poj-2985 题目大意:给你n只猫,有两种操作:1.将两只猫所在的小组合并.2.查询小组数第k大的小组的猫数. 注释:1<=n,m<=20 ...
- K:Union-Find(并查集)算法
相关介绍: 并查集的相关算法,是我见过的,最为之有趣的算法之一.并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.其相关的实现代码较为简短,实现思想也 ...
- 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS
原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...
- 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set
题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...
- poj2492(种类并查集/各种解法)
题目链接: http://poj.org/problem?id=2492 题意: 有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y, x, y表示教授判断x, y为 ...
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
C. Not Equal on a Segment 题目连接: http://www.codeforces.com/contest/622/problem/C Description You are ...
- 种类并查集——带权并查集——POJ1182;HDU3038
POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
随机推荐
- Python3 optparse模块
Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...
- iOS Autolayout 在tableView scrollView 适用 学习
1 如何自动适应cell的高度 autolayout 里面 使用 systemLayoutSizeFittingSize 方法 (系统通过 已知的完整的Constraints和view的属性来计算 ...
- mongodb php 支持
http://bbs.csdn.net/topics/391931404?page=1 windows下为php7.0.4安装目前官方版本对应的最新的php_mongodb.dll扩展,该扩展版本为1 ...
- Django---Blog系统开发之注册页面(验证码&ajax发送文件)
前端页面及渲染: 静态文件的配置:setting.py: static 文件放在app下 STATIC_URL = '/static/' STATIC_ROOT = ( os.path.join(BA ...
- Squid 安装
Squid简介 Squid是比较知名的代理软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟.目前使用Squid的用户也是十分广泛的.Squid与Linux下 ...
- 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)
系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...
- Hadoop相关知识整理系列之一:HBase基本架构及原理
1. HBase框架简单介绍 HBase是一个分布式的.面向列的开源数据库,它不同于一般的关系数据库,是一个适合于非结构化数据存储的数据库.另一个不同的是HBase基于列的而不是基于行的模式.HBas ...
- 使用iframe,注销以后,点击某个标签,跳转到的登录页面位于标签中
当使用iframe时,要么会话过期,要么手动注销,如果此时再进行操作,可能遇到跳转到登录页面,可是登录页面会位于标签执行,并没有全屏显示,即位于当前的iframe中,此时的解决方式很简单,只需要在登录 ...
- 什么是OOM?如何解决OOM问题!
1.什么是OOM? 程序申请内存过大,虚拟机无法满足我们,然后自杀了.这个现象通常出现在大图片的APP开发,或者需要用到很多图片的时候.通俗来讲就是我们的APP需要申请一块内存来存放图片的时候,系统认 ...
- 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)
题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...