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 ...
随机推荐
- Morley's Theorem (计算几何基础+向量点积、叉积、旋转、夹角等+两直线的交点)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- zabbix的命令执行
1.对于低版本的可用下列exp直接打到用户 http://119.29.48.232/zabbix/httpmon.php?applications=2 and (select 1 from (sel ...
- 【CSS】凹槽的写法
效果图: 实例代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...
- python基础===通过菲波那契数列,理解函数
def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.&quo ...
- 64_j1
JSCookMenu-2.0.4-13.fc26.noarch.rpm 13-Feb-2017 22:06 38098 Java-WebSocket-1.3.1-0.2.git58d1778.fc24 ...
- 64_g4
gnatcoll-2014-10.fc26.x86_64.rpm 28-Feb-2017 17:44 1738266 gnatcoll-devel-2014-10.fc26.i686.rpm 28-F ...
- Spring,tk-mapper源码阅读
Mybatis的源码学习(一): 前言: 结合spring本次学习会先从spring-mybatis开始分析 在学习mybatis之前,应该要对spring的bean有所了解,本文略过 先贴一下myb ...
- 高性能网络编程(1)—accept建立连接(待研究)
阿里云博客上一篇感觉还不错的文章,待研究,原文链接如下: http://blog.aliyun.com/673?spm=5176.7114037.1996646101.3.oBgpZQ&pos ...
- 简述MapReduce计算框架原理
1. MapReduce基本编程模型和框架 1.1 MapReduce抽象模型 大数据计算的核心思想是:分而治之.如下图所示.把大量的数据划分开来,分配给各个子任务来完成.再将结果合并到一起输出.注: ...