bzoj2733 永无乡 splay树的启发式合并
https://vjudge.net/problem/HYSBZ-2733
给一些带权点,有些点是互相连通的,
然后给出2种操作,在两点间加一条边,或者询问一个点所在的连通块内的第k小值的编号
并查集辅助+splay的启发式合并就行
由于结构简单,动态开点线段树合并也可以做
我写的是splay,由于一个奇怪的bug,我一气之下把之前的核心代码里的我自己写的splay和rotate代码全换成板子了

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
int casn,n,m,k;
vector<int> pre;
int find(int now) {return pre[now]==now?now:pre[now]=find(pre[now]);}
vector<int> val;
class splaytree{public:
#define nd node[now]
#define ndl node[node[now].son[0]]
#define ndr node[node[now].son[1]]
struct splaynode{
int son[2],fa,val,size;
splaynode(){size=1,fa=son[0]=son[1]=0;}
};
int cnt,root;
vector<splaynode> node;
inline void pushup(int now){nd.size=ndl.size+ndr.size+1;}
inline void pushdown(int now){}
inline int wh(int now){return node[nd.fa].son[1]==now;}
void rotate(int now){
int fa=nd.fa,gf=node[fa].fa,c=wh(now);
pushdown(fa);pushdown(now);
if(gf) node[gf].son[wh(fa)]=now;
nd.fa=gf;
node[fa].son[c]=nd.son[c^1];
node[node[fa].son[c]].fa=fa;nd.son[c^1]=fa;node[fa].fa=now;
pushup(fa);pushup(now);
}
void splay(int now,int dst=0){
for(;nd.fa!=dst;rotate(now))
if(node[nd.fa].fa!=dst)rotate(wh(now)==wh(nd.fa)?nd.fa:now);
if(!dst) root=now;
}
void insert(int pos){
int now=root,fa=0,val=node[pos].val;
while(now) fa=now,now=val<nd.val?nd.son[0]:nd.son[1];
now=pos;
node[fa].son[val>node[fa].val]=now;
nd.fa=fa;
splay(now);
}
void order(int now){
int l=nd.son[0],r=nd.son[1];
nd.son[0]=nd.son[1]=nd.fa=0;
nd.size=1;
if(l) order(l);
insert(now);
if(r) order(r);
}
void merge(int a,int b){
if(a==b) return ;
splay(a);splay(b);
if(node[a].size>node[b].size) swap(a,b);
pre[a]=b;root=b;
order(a);
}
int kth(int now,int k){
splay(now);int lsize=0;
while(now){
int lsum=lsize+ndl.size;
if(k<=lsum) now=nd.son[0];
else if(k==lsum+1) return now;
else lsize=lsum+1,now=nd.son[1];
}
return -1;
}
splaytree(int n){
node.resize(n+7,splaynode());
rep(i,1,n) node[i].val=val[i];
node[0].size=0;
root=0,cnt=0;
}
};
int main() {
IO;
int a,b;
cin>>n>>m;
val.resize(n+2);
pre.resize(n+2);
rep(i,1,n) cin>>val[i],pre[i]=i;
splaytree tree(n);
while(m--) {
cin>>a>>b;
a=find(a),b=find(b);
tree.merge(a,b);
}
cin>>m;string s;
while(m--){
cin>>s>>a>>b;
if(s=="B") {
a=find(a),b=find(b);
tree.merge(a,b);
}else{
a=find(a);
cout<<tree.kth(a,b)<<endl;
}
}
return 0;
}
bzoj2733 永无乡 splay树的启发式合并的更多相关文章
- BZOJ2733 永无乡【splay启发式合并】
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【BZOJ-2733】永无乡 Splay+启发式合并
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2048 Solved: 1078[Submit][Statu ...
- [BZOJ2733] [HNOI2012] 永无乡 (splay启发式合并)
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
- BZOJ2733 永无乡 【splay启发式合并】
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 4190 Solved: 2226 [Submit][Sta ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...
- bzoj2733: [HNOI2012]永无乡 线段树合并
永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...
- 【bzoj2733】[HNOI2012]永无乡 线段树合并
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- sigsuspend()阻塞:异步信号SIGIO为什么会被截胡?
关键词:fcntl.fasync.signal.sigsuspend.pthread_sigmask.trace events. 此文主要是解决问题过程中的记录,内容有较多冗余.但也反映解决问题中用到 ...
- PHP性能优化:in_array和isset 在大数组查询中耗时相差巨大,以及巧妙使用array_flip
今天在PHP业务开发中,发现了一个问题. 两个较大数组(20万+元素),遍历其中一个$a,另一个数组$b用于查找元素. 比如 foreach($a as $val){ if(in_array($xx, ...
- FineUI十周年纪念版即将发布(基于像素的响应式布局,独此一家)!
[新版预报]FineUI十周年纪念版(v5.0.0)即将于2018-04-23发布! 官网示例已更新:http://pro.fineui.com/ 特别助攻:基于像素的响应式布局,FineUI独家秘笈 ...
- Neutron flat network 学习
flat network 是不带 tag 的网络,要求宿主机的物理网卡直接与 linux bridge 连接,这意味着: 每个 flat network 都会独占一个物理网卡. 在 ML2 配置中 ...
- 我遇到的Spring的@Value注解失效问题
项目使用的是SSM体系,spring的配置如下,配置没问题,因为我发现其他文件中的@Value可以使用,只有一处@Value失效了. spring-servlet.xml <?xml versi ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- java从Swagger Api接口获取数据工具类
- PHP 5.6 中 Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version
解决方法 找到php.ini 文件, 把always_populate_raw_post_data 修改为-1 就行了. always_populate_raw_post_data=-1
- [M$]微软提供的ProcessExplorer等系统工具集合
https://docs.microsoft.com/en-us/sysinternals/downloads/index
- 继续沿用旧的网络访问模式Apache HTTP 客户端,防止Android9闪退
注意位置,在application 节点里面.