【题目链接】 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+并查集)的更多相关文章

  1. BZOJ 2733: [HNOI2012]永无乡 启发式合并treap

    2733: [HNOI2012]永无乡 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树

    Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...

  3. BZOJ 2733: [HNOI2012]永无乡(treap + 启发式合并 + 并查集)

    不难...treap + 启发式合并 + 并查集 搞搞就行了 --------------------------------------------------------------------- ...

  4. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  5. Bzoj 2733: [HNOI2012]永无乡 数组Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3955  Solved: 2112[Submit][Statu ...

  6. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  7. bzoj2733: [HNOI2012]永无乡 启发式合并

    地址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 题目: 2733: [HNOI2012]永无乡 Time Limit: 10 Sec   ...

  8. bzoj 2733: [HNOI2012]永无乡 离线+主席树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1167  Solved: 607[Submit][Status ...

  9. bzoj 2733: [HNOI2012]永无乡 -- 线段树

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...

  10. 线段树合并+并查集 || BZOJ 2733: [HNOI2012]永无乡 || Luogu P3224 [HNOI2012]永无乡

    题面:P3224 [HNOI2012]永无乡 题解: 随便写写 代码: #include<cstdio> #include<cstring> #include<iostr ...

随机推荐

  1. Lua中调用C++方法

    目前项目,使用了Lua脚本,至于使用Lua的好处不再赘述了.于是对Tolua做了一些小小的学习,总结一下吧. 主要说一下如何在Lua中调用C++方法. Lua调用C++的桥梁,是tolua.tolua ...

  2. Spring与MyBatis的整合(山东数漫江湖)

    首先看一下项目结构图: 具体步骤如下: 1.建立JDBC属性文件 jdbc.properties (文件编码修改为 utf-8 ) driver=com.mysql.jdbc.Driver url=j ...

  3. 爬虫--requests讲解

    什么是requests? Requests是用Python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全 ...

  4. 获取天气api

    http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100通过城市id获得天气数据,xml文件数据,当错误时会有<error>节点http: ...

  5. Python 关于时间和日期函数使用 -- (转)

    python中关于时间和日期函数有time和datatime   1.获取当前时间的两种方法: import datetime,time now = time.strftime("%Y-%m ...

  6. Linux中source命令的用法

    source命令: source命令也称为“点命令”,也就是一个点符号(.).source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录.因为linux所有的操作都会变成文 ...

  7. 查看服务器是否被DDOS攻击的方法

    伴随着现代互联网络快速发展,更加容易出现被攻击.尤其是ddos攻击已经不在是大网站需要关心的事情了.不少中小型企业,也在遭受ddos攻击.站长对ddos攻击不了解,所以网站被ddos攻击的时候,都不会 ...

  8. Optimizing subroutine calls based on architecture level of called subroutine

    A technique is provided for generating stubs. A processing circuit receives a call to a called funct ...

  9. 增大dma的分配

    前言 项目中需要通过驱动与fpga通讯,获取fpga往内存里写的数据.因为数据量比较大,需要驱动分配600多M的内存给fpga来写数据,且因为是与fpga通讯,需要连续的内存,还得是uncached的 ...

  10. golang相关问题

          [转载][翻译]Go的50坑:新Golang开发者要注意的陷阱.技巧和常见错误[1] Golang作为一个略古怪而新的语言,有自己一套特色和哲学.从其他语言转来的开发者在刚接触到的时候往往 ...