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 ...
随机推荐
- Http跨域时候预检没通过的几种原因
网上大多数涉及的原因(直接复制粘帖): CORS把HTTP请求分成两类,不同类别按不同的策略进行跨域资源共享协商. 1. 简单跨域请求. 当HTTP请求出现以下两种情况时,浏览器认为是简单跨域请求: ...
- 【洛谷 P2513】 [HAOI2009]逆序对数列(DP)
题目链接 这种求方案数的题一般都是\(dp\)吧. 注意到范围里\(k\)和\(n\)的范围一样大,\(k\)是完全可以更大的,到\(n\)的平方级别,所以这暗示了我们要把\(k\)写到状态里. \( ...
- TensorFlow 模型保存和导入、加载
在TensorFlow中,保存模型与加载模型所用到的是tf.train.Saver()这个类.我们一般的想法就是,保存模型之后,在另外的文件中重新将模型导入,我可以利用模型中的operation和va ...
- 如何升级nodejs版本 安装n模块报错 npm ERR! notsup Unsupported platform
如何升级nodejs版本 首先安装n模块, 输入npm install -g n n模块专门用来管理nodejs的版本. 如果出现npm ERR! notsup Unsupported platfor ...
- 浅谈JobExecutionContext与JobDataMap
1.JobExecutionContext简介 (1)当Scheduler调用一个Job,就会将JobExecutionContext传递给job的execute方法 quartz无法调用job的有参 ...
- 通过JDBC连接HiveServer2
如果通过JDBC连接HiveServer2时提示:User: hive is not allowed to impersonate hive,需要在core-site.xml中新增如下配置: hado ...
- libsensor
https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...
- Deep Learning基础--26种神经网络激活函数可视化
在神经网络中,激活函数决定来自给定输入集的节点的输出,其中非线性激活函数允许网络复制复杂的非线性行为.正如绝大多数神经网络借助某种形式的梯度下降进行优化,激活函数需要是可微分(或者至少是几乎完全可微分 ...
- PHP常用函数总结(180多个)
PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...
- mycncart 前台代码跟踪
1.进入根目录的入口文件,index.php require_once(DIR_SYSTEM . 'startup.php');//最为重要的一步 start('catalog');//执行了这个方法 ...