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. MySQL数据库(2)_MySQL数据库和数据库表操作语句

    一.关于数据库操作的sql语句 -- .创建数据库(在磁盘上创建一个对应的文件夹) create database [if not exists] db_name [character set xxx ...

  2. Loadrunner之脚本篇——事务函数

    1.事务的开始和结束名称需要相同 lr_start_transaction(“transaction_name”); …//事务处理 lr_end_transaction(“transaction_n ...

  3. 静态库引入引起的错误解决方案,ld: warning: ignoring file ”…/XXX.a”, file was built for archive which is not the architecture being linked (armv7): “…/XXX.a” Undefined symbols for architecture armv7: "_OBJC_CLASS_$

    想目中不免会引入一些静态库,可是有时加入'.a'文件后编译便会报以下错误 ld: warning: ignoring file ”…/XXX.a”, file was built for archiv ...

  4. centos6 没有eth0网络

    编辑 /etc/sysconfig/network-scripts/ifcfg-eth0 ONREBOOT=no #改成yes service network restart 在用 ifconfig ...

  5. JSP笔记03——环境搭建(转)

    不完全翻译,结合谷歌,一定主观性,还可能有误,原始内容地址:https://www.tutorialspoint.com/jsp/jsp_environment_setup.htm [注释]这篇貌似有 ...

  6. Python编程-常用模块及方法

    常用模块介绍 一.time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行 ...

  7. Java 封装、继承、多态

    Java中使用 extends 关键字 进行父类继承 在初始化子类时,子类会自动执行父类的构造方法, 如果子类的构造方法中没有显示调用父类的构造方法, 则系统会默认调用父类无参的构造方法 super( ...

  8. Python 字符串概念和操作

    # 字符串概念:由单个字符串组成的一个集合 # 普通字符串(非原始字符串) str = "abc" print(str) # abc # 原始字符串(前面加r) str = r&q ...

  9. C#遍历指定文件夹中的所有文件

    DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryInfo NextFolder in ...

  10. CodeForces 266E More Queries to Array...(线段树+式子展开)

    开始觉得是规律题的,自以为是的推了一个规律,结果测试数据都没过....看了love神的博客才发现只是把式子展开就找到规律了.不过挺6的是我虽然想错了,但是维护的的东西没有错,只是改改(改了进两个小时好 ...