POJ 题目2985 The k-th Largest Group(线段树单点更新求第k大值,并查集)
来源:互联网 发布:四川大学口腔医学知乎 编辑:程序博客网 时间:2024/05/17 22:25
Description
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 catj is in, thus creating a new group. On top of that, Newman wants to know the size of thek-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. IfC = 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); IfC = 1, then there is only one number k (1 ≤ k ≤ the current number of groups) following indicating Newman wants to know the size of thek-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 100 1 21 40 3 41 20 5 61 10 7 81 10 9 101 1
Sample Output
12222
Hint
When there are three numbers 2 and 2 and 1, the 2nd largest number is 2 and the 3rd largest number is 1.
Source
POJ Monthly--2006.08.27, zcgzcgzcg
题目大意:n只猫,编号1~n,m个操作,0 i j意思是把i在的组并到j,1 k的意思是求第k大组的大小
注释的是我的思路,这个写法是参照别人的思路,他的比我的稍微快个100来ms,
ac代码
#include<stdio.h>#include<string.h>int pre[200020],num[200020];int node[200020<<2],n,m;void build_tr(int l,int r,int tr){if(l==1)node[tr]=n;elsenode[tr]=0;if(l==r){/*if(l==1)node[tr]=n;elsenode[tr]=0;*/return;}int mid=(l+r)>>1;build_tr(l,mid,tr<<1);build_tr(mid+1,r,tr<<1|1);//node[tr]=node[tr<<1]+node[tr<<1|1];}void init(int n){int i;for(i=1;i<=n;i++){num[i]=1;pre[i]=i;}}int find(int x){if(x==pre[x])return x;return pre[x]=find(pre[x]);}void update(int pos,int add,int l,int r,int tr){node[tr]+=add;if(l==r){//node[tr]+=add;return;}int mid=(l+r)>>1;if(pos<=mid){update(pos,add,l,mid,tr<<1);}elseupdate(pos,add,mid+1,r,tr<<1|1);//node[tr]=node[tr<<1]+node[tr<<1|1];}int query(int k,int l,int r,int tr){if(l==r){return l;}int mid=(l+r)>>1;if(node[tr<<1|1]>=k)query(k,mid+1,r,tr<<1|1);elsequery(k-node[tr<<1|1],l,mid,tr<<1);}int main(){//int n,m;while(scanf("%d%d",&n,&m)!=EOF){build_tr(1,n,1);init(n);int i;for(i=0;i<m;i++){int op;scanf("%d",&op);if(!op){int a,b;scanf("%d%d",&a,&b);int fa=find(a),fb=find(b);if(fa!=fb){pre[fa]=fb;update(num[fa],-1,1,n,1);update(num[fb],-1,1,n,1);update(num[fa]+num[fb],1,1,n,1);num[fb]+=num[fa];}}else{int a;scanf("%d",&a);printf("%d\n",query(a,1,n,1));}}}}
- POJ 题目2985 The k-th Largest Group(线段树单点更新求第k大值,并查集)
- poj 2985 The k-th Largest Group 并查集+树状数组求第k大
- POJ 2985The k-th Largest Group 线段树求整体第K大
- poj 2985 并查集+线段树 线段树求第k大数 The k-th Largest Group
- poj 2985 The k-th Largest Group (并查集x全局动态第k大)
- poj 2985 The k-th Largest Group(线段树+并查集)
- POJ 2985 The k-th Largest Group 第k大数 Treap / 树状数组 + 并查集
- The k-th Largest (并查集+线段树)
- Treap树(并查集 + 树堆)POJ —— 2985 The k-th Largest Group
- PKU2985(The k-th Largest Group)线段树+并查集
- poj 2985 The k-th Largest Group/并查集+sbt
- poj 2985 The k-th Largest Group (Treap+并查集)
- POJ 2985:The k-th Largest Group 树状数组求第K小的元素
- POJ2985 The k-th Largest Group(treap+并查集)
- POJ 2985 The k-th Largest Group
- Poj 2985 The k-th Largest Group
- POJ-2985-The k-th Largest Group
- POJ 2985 The k-th Largest Group
- 南邮 OJ 1016 求幂
- extjs类继承图之数据源(2)
- Version of activiti database (5.15.1) is more recent than the engine (5.14)
- 如何做实时监控Spring Boot服务
- word转pdf好用的转换器分享
- POJ 题目2985 The k-th Largest Group(线段树单点更新求第k大值,并查集)
- ajax调用后台异常,页面跳转
- block为什么用copy关键字
- 升级 Windows 10 后 SVN 图标不显示的解决办法
- 黑马程序员--学习OC面向对象特性
- 读取独立配置的properties工具
- 个人笔记
- uva 11731(旁切圆)
- python的一些机器学习算法库