【题目链接】 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. C++ Vector 中自定义对象的排序

    需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...

  2. 程序员你为什么这么累? - Controller规范

    导读:程序员你为什么这么累? 接口定义:程序员你为什么这么累? - 接口定义 第一篇文章中,我贴了2段代码,第一个是原生态的,第2段是我指定了接口定义规范,使用AOP技术之后最终交付的代码,从15行到 ...

  3. Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  4. 深入理解 JavaScript(五)

    根本没有“JSON 对象”这回事! 前言 写这篇文章的目的是经常看到开发人员说:把字符串转化为 JSON 对象,把 JSON 对象转化成字符串等类似的话题,所以把之前收藏的一篇老外的文章整理翻译了一下 ...

  5. js_判断当前url是否合法http(s)

    alert(checkURL('http:555')); //false function checkURL(URL) { var str = URL, Expression = /http(s)?: ...

  6. git 配置多用户

    .ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...

  7. 广度优先算法(BFS)与深度优先算法(DFS)

    一.广度优先算法BFS(Breadth First Search) 基本实现思想 (1)顶点v入队列. (2)当队列非空时则继续执行,否则算法结束. (3)出队列取得队头顶点v: (4)查找顶点v的所 ...

  8. Codeforces Round #483 (Div. 1) 简要题解

    来自FallDream的博客,未经允许,请勿转载,谢谢. 为了证明一下我又来更新了,写一篇简要的题解吧. 这场比赛好像有点神奇,E题莫名是道原题,导致有很多选手直接过掉了(Claris 表演24s过题 ...

  9. Exception 和 Error 包结构

  10. ssh日常优化使用

    config文件的使用 ssh命令默认会加载 ~/.ssh/config 文件作为配置文件,如果没有则采用默认配置.如果我们想要对ssh进行定制,那么就可以使用如下方法 [root@linux-nod ...