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 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
随机推荐
- 解决 Intellij IDEA Cannot Resolve Symbol ‘XXX’ 问题
1.java类报错 https://blog.csdn.net/qq_32040767/article/details/77096680 2.类对应的依赖没有加载进来.编译器自身的设置和缓存问题类. ...
- [看图说话]在VMware Workstation 9中安装Mac OS X 10.8 Mountain Lion
本文环境: CPU:Intel Core i7 920: OS:Windows 7: 内存:8G: 玩Hackintosh各有各的理由,不管什么理由,利用虚拟机安装Mac OS X都是一个可行的办法. ...
- WPF: 自动设置Owner的ShowDialog 适用于MVVM
原文:WPF: 自动设置Owner的ShowDialog 适用于MVVM 原文地址:http://www.mgenware.com/blog/?p=339 WPF中的Windows的ShowDialo ...
- Gruntfile.js模板
module.exports = function(grunt) { // 配置项 var AppConfig = { name: 'app', //源文件目录 src: 'app/src', //生 ...
- 二十八、layui的日历组件使用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- OracleSql语句学习(一)
--SQL语句本身是不区分大小写的,每个关键字用空格隔开,为了增加可读性,退出所有关键字--全部大写,非关键字都小写SELECT SYSDATE FROM dual--创建表CREATE TABLE ...
- PyQt5基础应用一
一.PyQt5基础 1.1 创建窗口 import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__ ...
- Django配置404页面
一.settings配置 1.首先需要在settings中将DEBUG由原来的True改为False DEBUG = False 2.需要设置 ALLOWED_OSTS = ["*" ...
- locust安装及其简单使用----基于python的性能测试工具
1.已安装python3.6 ,安装步骤略 pip安装: pip install locust 检查locust是否安装成功 locust --help 2.安装 pyzmq If you inten ...
- Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...