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.

并查集+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)的更多相关文章

  1. 51 nod 1456 小K的技术(强连通 + 并查集)

    1456 小K的技术 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   苏塞克王国是世界上创新技术的领先国家,在王国中有n个城市 ...

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

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

  3. [poj-2985]The k-th Largest Group_Treap+并查集

    The k-th Largest Group poj-2985 题目大意:给你n只猫,有两种操作:1.将两只猫所在的小组合并.2.查询小组数第k大的小组的猫数. 注释:1<=n,m<=20 ...

  4. K:Union-Find(并查集)算法

    相关介绍:  并查集的相关算法,是我见过的,最为之有趣的算法之一.并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.其相关的实现代码较为简短,实现思想也 ...

  5. 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS

    原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...

  6. 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set

    题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...

  7. poj2492(种类并查集/各种解法)

    题目链接: http://poj.org/problem?id=2492 题意: 有t组测试数据, 对于每组数据,第一行n, m分别表示昆虫的数目和接下来m行x, y, x, y表示教授判断x, y为 ...

  8. 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 ...

  9. 种类并查集——带权并查集——POJ1182;HDU3038

    POJ1182 HDU3038 这两个题比较像(一类题目),属于带权(种类)并查集 poj1182描绘得三种动物种类的关系,按照他一开始给你的关系,优化你的种类关系网络,最后看看再优化的过程中有几处矛 ...

随机推荐

  1. OpenGL学习进程(7)第五课:点、边和图形(二)边

    本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概 ...

  2. 【HackerRank】 Sherlock and The Beast

    Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...

  3. awk中打印连续多列,或者删除多列的技巧

    问题:比如有一个文件是20列,你只要后面的18列,怎么打印. 方法:把第一列和第二列做空:用print打印 [wangjq@mgmt humandb]$ cat test 1 2 3 4 5 6 7 ...

  4. Java Collection API

    在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...

  5. INSPIRED启示录 读书笔记 - 第37章 大众网络服务产品

    十大要点 1.可用性:大众网络服务产品必须具备良好的用户体验 2.人物角色:按典型特征将用户分类,抽象出有代表性的用户类型(人物角色) 3.扩展性:应该不间断地考虑扩展性问题,永远留有余地,不到万不得 ...

  6. Eclipse开发快捷键精选

    1.alt+?或alt+/:自动补全代码或者提示代码2.ctrl+o:快速outline视图3.ctrl+shift+r:打开资源列表4.ctrl+shift+f:格式化代码5.ctrl+e:快速转换 ...

  7. WebUploader API文档

    Web Uploader内部类的详细说明,以下提及的功能类,都可以在WebUploader这个变量中访问到. As you know, Web Uploader的每个文件都是用过AMD规范中的defi ...

  8. 通过join方法顺序执行多个线程

    方法一:直接用多线程之间的通讯去解决 package com.toov5.test; import javax.imageio.ImageTypeSpecifier; class Res1{ char ...

  9. Codeforces Beta Round #27 (Codeforces format, Div. 2) E. Number With The Given Amount Of Divisors 反素数

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

  10. python的map与reduce与filter

    map(f, Itera)  # 对每一个元素都使用f(x) >>> sq = lambda x:x**2 >>> l = map(sq,[-1,0,1,2,-3] ...