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描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...
随机推荐
- 前端基础之JavaScript_(1)_ECMAScript
一.JavaScript概述 JavaScript的历史 992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase. ...
- 面向服务体系架构(SOA)和数据仓库(DW)的思考
摘要: 当前业界对面向服务体系架构(SOA)和数据仓库(Data Warehouse,DW)都介绍的很多,提出了很多优秀的解决方案,但是一般是把 SOA 和 DW 单独考虑,SOA 和 DW 有着共同 ...
- Understanding When to use RabbitMQ or Apache Kafka
https://content.pivotal.io/rabbitmq/understanding-when-to-use-rabbitmq-or-apache-kafka How do humans ...
- Spring 全局异常捕捉
Spring全局异常捕捉类 注解@ControllerAdvice package com.sicdt.sicsign.web.bill.controller; import org.springfr ...
- 移动端tap或touch类型事件的点透问题认识
1.什么是点透? 举例说明:下图B元素是黄色方块,B元素中包含了C元素,C元素是一个a链接,本身自带click事件按,然后又一个半透明的粉色元素A遮盖在B元素上(看图中A元素是覆盖在B元素上的,不然B ...
- samtools+bcftools 进行SNP calling
两个软件的作用:1.samtools mpileup 主要是用于收集BAM文件中的信息,这个位点上有多少条read匹配,匹配read的碱基是什么,并将这些信息存储在BCF文件中.2.bcftools ...
- memcpy与memmove
函数原型: void* memcpy(void *dst,void const *src,size_t count) void* memmove(void *dst,void const *src,s ...
- log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF > FATAL ...
- 用vim写python脚本的自动缩进格式设置
- 【P1274】魔术数字游戏(搜索+剪枝+模拟)
做完了这个题的我一口老血喷在屏幕上... 这个题难度不高(~~胡扯~~),就是爆搜就可以了,然而..判断条件灰常多,剪枝也就非常多..然而,这些判断条件又不得不必须满足,所以也就十分容易错... 说一 ...