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. Nginx反向代理+负载均衡简单实现

    一.基础环境: 负   载  机:A机器: 192.168.71.223后端机器1:B机器:192.168.71.224后端机器2:C机器:192.168.71.226 需求: 1)访问A机器的808 ...

  3. Django 项目补充知识(JSONP,前端瀑布流布局,组合搜索,多级评论)

    一.JSONP 1浏览器同源策略 通过Ajax,如果在当前域名去访问其他域名时,浏览器会出现同源策略,从而阻止请求的返回 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一 ...

  4. 014_HDFS存储架构、架构可靠性分析、副本放置策略、各组件之间的关系

    1.HDFS存储架构

  5. Linux Shell基础 多个命令中的分号(;)、与(&&) 、 或(||)

    概述 在 Bash 中,如果需要让多条命令按顺序执行,则有这样方法,如表 1 所示. 多命令执行符 格 式 作 用 : 命令1 ; 命令2 多条命令顺序执行,命令之间没有任何逻辑关系 &&am ...

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

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

  7. linux输入子系统简述【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7678035 1,linux输入子系统简述 其实驱动这部分大多还是转载别人的,linux ...

  8. ElasticSearch入门常用命令

    基于开源项目MyAlice智能客服学习ElasticSearch https://github.com/hpgary/MyAlice/wiki/%E7%AC%AC01%E7%AB%A0%E5%AE%8 ...

  9. R语言笔记001——读取csv格式数据

    读取csv格式数据 数据来源是西南财经大学 司亚卿 老师的课程作业 方法一:read.csv()函数 file.choose() read.csv("C:\\Users\\Administr ...

  10. Freemarker 使用

    Freemarker 使用 博客分类: JAVA freemarker  以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个 ...