poj 2985 The k-th Largest Group (Treap+并查集)
来源:互联网 发布:java如何让窗口置顶 编辑:程序博客网 时间:2024/05/17 06:52
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 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 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
思路:Treap中存的是每个团队的猫的数量。每次合并的时候,是属于两个团队,那就将原来两个团队的数量从Treap中删除,然后在Treap中加入两个团队数量和,再用并查集合并两个团队。输出的时候,优先从右子树开始查找就行了。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX_N 200005using namespace std;int pre[MAX_N];bool t[MAX_N];int root,size;int Find(int x){ if(x!=pre[x]) pre[x]=Find(pre[x]); return pre[x];}int mix(int x,int y){ int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fy]=fx; return 1; } return 0;}struct data{ int l,r,v,w,size,rnd;}tr[MAX_N];void update(int k){ tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;}void rturn(int &k){ int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k; tr[t].size=tr[k].size;update(k);k=t;}void lturn(int &k){ int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k; tr[t].size=tr[k].size;update(k);k=t;}void insert(int &k,int x){ if(k==0) { size++;k=size; tr[k].w=tr[k].size=1;tr[k].rnd=rand(); tr[k].v=x; return; } tr[k].size++; if(x==tr[k].v) tr[k].w++; else if(x>tr[k].v) { insert(tr[k].r,x); if(tr[tr[k].r].rnd<tr[k].rnd) lturn(k); } else { insert(tr[k].l,x); if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k); }}void del(int &k,int x){ if(k==0) return; if(tr[k].v==x) { if(tr[k].w>1) { tr[k].w--;tr[k].size--;return ; } if(tr[k].l*tr[k].r==0) k=tr[k].l+tr[k].r; else if(tr[tr[k].l].rnd<tr[k].rnd) rturn(k),del(k,x); else lturn(k),del(k,x); } else if(x>tr[k].v) tr[k].size--,del(tr[k].r,x); else tr[k].size--,del(tr[k].l,x);}int query_num(int k,int x){ if(k==0) return 0; if(x<=tr[tr[k].r].size) return query_num(tr[k].r,x); else if(x>tr[tr[k].r].size+tr[k].w) return query_num(tr[k].l,x-tr[tr[k].r].size-tr[k].w); else return tr[k].v;}int res[MAX_N];int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { root=0,size=0; memset(res,0,sizeof(res)); for(int i=0;i<n;i++) { tr[i].l=0; tr[i].r=0; } for(int i=1;i<=n;i++) insert(root,1),pre[i]=i,res[i]=1; int c,x,y; for(int i=1;i<=m;i++) { scanf("%d",&c); if(c==0) { scanf("%d%d",&x,&y); if(Find(x)!=Find(y))//当两个猫是属于两个团队的时候才进行操作 { del(root,res[Find(x)]); del(root,res[Find(y)]); res[Find(x)]+=res[Find(y)]; mix(x,y); insert(root,res[Find(x)]); } } else { scanf("%d",&x); printf("%d\n",query_num(root,x)); } } } return 0;}
- POJ 2985 The k-th Largest Group 第k大数 Treap / 树状数组 + 并查集
- poj 2985 The k-th Largest Group (Treap+并查集)
- POJ2985 The k-th Largest Group(treap+并查集)
- Treap树(并查集 + 树堆)POJ —— 2985 The k-th Largest Group
- POJ 2985 The k-th Largest Group [并差集+treap]
- poj 2985 The k-th Largest Group 并查集+树状数组求第k大
- poj 2985 并查集+线段树 线段树求第k大数 The k-th Largest Group
- poj 2985 The k-th Largest Group/并查集+sbt
- poj 2985 The k-th Largest Group(线段树+并查集)
- POJ 2985 The k-th LargestGroup(Treap+并查集)
- POJ 2985 The k-th LargestGroup(Treap+并查集)
- poj 2985 The k-th Largest Group (并查集x全局动态第k大)
- [Treap]poj2985 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
- POJ 2985 The k-th Largest Group
- POJ 题目2985 The k-th Largest Group(线段树单点更新求第k大值,并查集)
- 欢迎使用CSDN-markdown编辑器
- Android Studio起步配置
- [OpenGL] Normal Mapping 法线映射
- 多线程的四种状态及同步机制的应用(火车票线程同步)
- UVa1629 Cake Slicing
- poj 2985 The k-th Largest Group (Treap+并查集)
- python3与2的区别
- Hbase shell 命令
- Spring事务管理
- linux系统服务状态
- 三(6)、JSP九大内置对象——application
- xml之JDOM解析(含乱码处理)
- ubuntu修改为阿里源
- 黑盒测试与白盒测试