BZOJ 2733 [HNOI2012]永无乡(启发式合并+Treap+并查集)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=2733
【题目大意】
  给出n个点,每个点都有自己的重要度,现在有连边操作和查询操作,
      查询操作要求找出一个连通块中重要度第k的点的id
【题解】
  我们用Treap维护每个连通块,对于连边操作,我们用启发式合并,
      将size比较小的Treap并入size比较大的Treap,同时用并查集维护连通信息
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=100010,M=N;
namespace Treap{
struct node{
int val,cnt,size,p,id;node *l,*r;
node(){}
node(int val,int id):val(val),id(id){p=rand();cnt=size=1;l=r=NULL;}
void up(){size=cnt;if(l)size+=l->size;if(r)size+=r->size;}
}*root[N],*pool[N];
int top;
node *new_node(){return pool[top++];}
void Initialize(){top=0;for(int i=0;i<N;i++)pool[i]=new node();}
void Rotatel(node*&x){node*y=x->r;x->r=y->l;x->up();y->l=x;y->up();x=y;}
void Rotater(node*&x){node*y=x->l;x->l=y->r;x->up();y->r=x;y->up();x=y;}
//往treap上插入点
void Insert(node*&x,node y){
if(!x){x=new_node();(*x)=y;return;}
x->size++;
if(y.val==x->val){x->cnt++;return;}
if(y.val<x->val){
Insert(x->l,y);
if(x->l->p>x->p)Rotater(x);
}else{
Insert(x->r,y);
if(x->r->p>x->p)Rotatel(x);
}
}
// 查找第k小的元素
int kth(node*x,int rnk){
while(x){
int d=x->l?x->l->size:0;
if(rnk<=d)x=x->l;
else if(rnk>d+x->cnt)rnk-=d+x->cnt,x=x->r;
else return x->id;
}return -1;
}
void Del_node(node*&u){pool[--top]=u;u=NULL;}
// 将B树并入A树
void merge(node*&A,node*&B){
if(!B)return;
if(B->l)merge(A,B->l);
if(B->r)merge(A,B->r);
Insert(A,*B); Del_node(B);
}
}
int n,m,k,u,v,q;
namespace Union_Find_Set{
int f[M],size[M],block;
void Initialize(){
memset(f,0,sizeof(f));
block=n;
}
int Find(int x){
if(!f[x])f[x]=x,size[x]=1;
if(f[x]==x)return x;
return f[x]=Find(f[x]);
}
void Union(int x,int y){
x=Find(x); y=Find(y);
if(x==y)return;
if(size[x]>size[y])swap(x,y);
f[x]=y; size[y]+=size[x];
block--;
}
}
void Heuristic_merge(int x,int y){
int fx=Union_Find_Set::Find(x);
int fy=Union_Find_Set::Find(y);
if(fx==fy)return;
if(Treap::root[fx]->size<Treap::root[fy]->size)swap(x,y),swap(fx,fy);
Treap::merge(Treap::root[fx],Treap::root[fy]);
Union_Find_Set::f[fy]=fx;
}
int main(){
using namespace Treap;
Initialize();
Union_Find_Set::Initialize();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&k);
Union_Find_Set::f[i]=i;
Insert(root[i],node(k,i));
}
while(m--){
scanf("%d%d",&u,&v);
Heuristic_merge(u,v);
}
scanf("%d",&q);
char op[5];
while(q--){
scanf("%s",op);
if(op[0]=='B'){
scanf("%d%d",&u,&v);
Heuristic_merge(u,v);
}else{
scanf("%d%d",&u,&v);
printf("%d\n",kth(root[Union_Find_Set::Find(u)],v));
}
}return 0;
}
BZOJ 2733 [HNOI2012]永无乡(启发式合并+Treap+并查集)的更多相关文章
- BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
		
2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
 - BZOJ 2733  [HNOI2012]永无乡 - 启发式合并主席树
		
Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...
 - BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)
		
不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...
 - BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
		
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
 - Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并
		
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3955 Solved: 2112[Submit][Statu ...
 - Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
		
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
 - bzoj2733: [HNOI2012]永无乡  启发式合并
		
地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec ...
 - bzoj 2733: [HNOI2012]永无乡 离线+主席树
		
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1167 Solved: 607[Submit][Status ...
 - bzoj 2733: [HNOI2012]永无乡  -- 线段树
		
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
 - 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡
		
题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...
 
随机推荐
- new操作符(翻译自mozilla.org)
			
翻译自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new new操作符可以实例化一个用户自 ...
 - sqlmap参数说明
			
--delay 设置每隔几秒测试一次注入 --safe-url 设置sqlmap要访问的正常url --safe-freq 设置每测试多少条注入语句后才去访问safe-url --code 设置能正常 ...
 - copy_from_user分析
			
前言 copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.它内部的实现当然不仅仅拷贝数据,还需要考虑到传入的用户空间地址是否有效,比如地址是不 ...
 - MACHINE_START与MACHINE_END【转】
			
转自:http://blog.csdn.net/cxw3506/article/details/8475965 版权声明:本文为博主原创文章,未经博主允许不得转载. 在移植Linux时,有个结构体需要 ...
 - device tree --- label
			
[label:] <device node name>[@ unit-address] 為 device node 取 label name, 可以在其它位置使用 &label 存 ...
 - 小程序2(JSSDK,ECS搭建ftp服务器)
			
JSSDK 开发步骤 绑定安全域名(域名绑定给任意一个公众号) 引入js 权限验证 wx.config({}) ready 所有的开发写在ready中 error 分享接口 onMenuShareTi ...
 - 动画基础--基于Core Animation(1)
			
1.简介 上一篇文章[New learn]动画-基于UIView了解到了一些直接由UIView这个在UIKIT提供的类中提供的一些动画方法. 使用UIView的动画特性已经能够满足我们很多的需求,它是 ...
 - 1.ubuntu的安装
			
分两种 1. 在VMware中安装,则与Centos的安装类似 2. 在VirtualBox里安装 --> 1. 先“新建” 一个虚拟电脑 2. 根据需求编辑虚拟电脑的信息 (具体的大小.内存等 ...
 - 将MongoDB安装成为Windows服务
			
使用以下命令将MongoDB安装成为Windows服务.笔者的MongoDB目录为D:\Program Files\mongodb 切换到D:\Program Files\mongodb\bin> ...
 - [ python ] 反射及item系列
			
反射 什么是反射? 通过字符串的形式操作对象相关属性.python中的事物都是对象: 关键方法: (1)getattr:获取属性 (2)setattr:设置属性 (3)hashattr:检测是否含有属 ...