POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
Time Limit: 2000MS | Memory Limit: 131072K | |
Total Submissions: 8807 | Accepted: 2875 |
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 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.
Source
并查集维护连通分量大小,树状数组求cc中第k大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=2e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,op,x,y,k;
int fa[N],size[N],tot=;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);} int c[N];
inline int lowbit(int x){return x&-x;}
inline void add(int p,int v){
for(;p<=n;p+=lowbit(p)) c[p]+=v;
}
inline int sum(int p){
int res=;
for(;p>;p-=lowbit(p)) res+=c[p];
return res;
}
inline int kth(int k){
int x=,cnt=;
for(int i=;i>=;i--){
x+=(<<i);
if(x>=n||cnt+c[x]>=k) x-=(<<i);
else cnt+=c[x];
}
return x+;
} int main(){
n=read();m=read();
for(int i=;i<=n;i++) fa[i]=i,size[i]=,tot++;
add(,n);
for(int i=;i<=m;i++){
op=read();
if(!op){
x=read();y=read();
int f1=find(x),f2=find(y);
if(f1!=f2){
fa[f1]=f2;
add(size[f1],-);
add(size[f2],-);
size[f2]+=size[f1];
add(size[f2],);
tot--;
}
// printf("%d %d %d %d\n",f1,f2,size[f1],size[f2]);
}else{
k=tot-read()+;//printf("k %d\n",k);
printf("%d\n",kth(k));
}
}
}
当然treap也可以 注意是第k大
//
// main.cpp
// poj2985_treap
//
// Created by Candy on 27/11/2016.
// Copyright © 2016 Candy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lc t[x].l
#define rc t[x].r
const int N=2e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,op,x,y,k;
int fa[N],size[N];
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
struct node{
int l,r,v,w,rnd,size;
}t[N];
int cnt,root;
inline void update(int x){t[x].size=t[lc].size+t[rc].size+t[x].w;}
inline void rturn(int &x){
int c=lc;lc=t[c].r;t[c].r=x;
t[c].size=t[x].size;update(x);x=c;
}
inline void lturn(int &x){
int c=rc;rc=t[c].l;t[c].l=x;
t[c].size=t[x].size;update(x);x=c;
}
void ins(int &x,int v){//printf("ins %d %d\n",x,v);
if(x==){
cnt++;x=cnt;
t[cnt].l=t[cnt].r=;t[cnt].w=t[cnt].size=;
t[cnt].v=v;t[cnt].rnd=rand();
}else{
t[x].size++;
if(t[x].v==v) t[x].w++;
else if(v<t[x].v){
ins(lc,v);
if(t[lc].rnd<t[x].rnd) rturn(x);
}else{
ins(rc,v);
if(t[rc].rnd<t[x].rnd) lturn(x);
}
}
}
void del(int &x,int v){
if(x==) return;
if(t[x].v==v){
if(t[x].w>){t[x].w--;t[x].size--;return;}
if(lc*rc==) x=lc+rc;
else if(t[lc].rnd<t[rc].rnd) rturn(x),del(x,v);
else lturn(x),del(x,v);
}else{
t[x].size--;
if(v<t[x].v) del(lc,v);
else del(rc,v);
}
}
//int kth(int x,int k){
// if(x==0)return 0;
// if(k<=t[lc].size) return kth(lc,k);
// else if(k>t[lc].size+t[x].w) return kth(rc,k-t[lc].size-t[x].w);
// else return t[x].v;
//}
int kth(int x,int k){
if(x==) return ;
if(k<=t[rc].size) return kth(rc,k);
else if(k>t[rc].size+t[x].w) return kth(lc,k-t[rc].size-t[x].w);
else return t[x].v;
}
int main(){
n=read();m=read();
for(int i=;i<=n;i++) fa[i]=i,size[i]=;
while(m--){
op=read();
if(!op){
x=read();y=read();
int f1=find(x),f2=find(y);
if(f1!=f2){
fa[f1]=f2;
if(size[f1]!=) del(root,size[f1]);
if(size[f2]!=) del(root,size[f2]);
size[f2]+=size[f1];
ins(root,size[f2]);
}
}else{
k=read();//printf("kth %d %d\n",k,t[root].size);
if(k>t[root].size) puts("");
else printf("%d\n",kth(root,k));
}
}
}
POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]的更多相关文章
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- *HDU2852 树状数组(求第K小的数)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 树状数组求第K大(From CLJ)
; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;
- POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2691 Accepted: 996 Descript ...
随机推荐
- 背水一战 Windows 10 (13) - 绘图: Stroke, Brush
[源码下载] 背水一战 Windows 10 (13) - 绘图: Stroke, Brush 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Stroke - 笔划 Brush ...
- Java--设计模式心得体会
1.策略模式: 策略模式就是将能够通用的算法,封装成不同的组件,实现同一个接口,使之可以互换. 例子:SpringMVC的9大组件,都采用策略模式.比如HandlerMethodArgumentRes ...
- 利用SCORE法则来总结一次偷懒的单元测试过程
最近遇到一个单元测试的问题,本周正好学个了一个SCORE法则,这里正好练练手应用此法则将问题的前因后果分享给大家. S:背景 代码要有单元测试,检测的标准就是统计代码的单元测试覆盖率,程序员需要达到 ...
- javascript组合继承
javascript继承有几种继承方式,现在来说说其中的组合继承. 组合继承是结合了原型链和借用构造函数这两种技术的继承方式,分别利用它们的长处,避免了短处.那就先说说这两种技术吧. 原型链 原型链 ...
- vs2012使用64位IIS EXPRESS调试
使用Visual Studio 2012开发网站或web应用程序时,可以使用两种web server.自带的development server和IIS EXPRESS.development ser ...
- SharePoint 2013 Designer系列之数据视图筛选
在SharePoint中,我们经常需要对列表进行简单的筛选,这时,数据视图就有作用了,我们可以定制对于字段的筛选,来进行展示:特别的,筛选不同于搜索,并没有对于附件或者文档的全文检索,如果需要全文检索 ...
- SharePoint 2013 必备组件之 Windows Server AppFabric 安装错误
1.如下图,在使用SharePoint2013产品准备工具的时候,网上下载安装Windows Server AppFabric的时候,报错,点击完成重启计算机,重新安装依然报错. 2.无奈之下,只有选 ...
- 将oracle冷备份恢复到另外一个数据库实例中
因更换服务器需要将Oracle数据库转移到另外台Oracle中.说明: 1.测试环境为:windows server2003 和 oracle 10g. 2.2台服务器安装的程序目录一样,数据目录不一 ...
- 用C#缩小照片上传到各种空间
中秋到了,首先祝各位猿友节日快乐!!! 本博文的原名称是“跟我一起用C#压缩照片上传到各种空间”,评论上有人开骂,没办法我这人就是自信霸气,但是既然有人提出来我还是改掉吧,如果文章写得不好的地方欢迎大 ...
- 用Kotlin开发Android应用(II):创建新项目
这是关于Kotlin的第二篇.各位高手发现问题,请继续“拍砖”. 原文标题:Kotlin for Android(II): Create a new project 原文链接:http://anton ...