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树的启发式合并的更多相关文章

  1. BZOJ2733 永无乡【splay启发式合并】

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  2. 【BZOJ-2733】永无乡 Splay+启发式合并

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2048  Solved: 1078[Submit][Statu ...

  3. [BZOJ2733] [HNOI2012] 永无乡 (splay启发式合并)

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

  4. BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

  5. BZOJ2733 永无乡 【splay启发式合并】

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 4190  Solved: 2226 [Submit][Sta ...

  6. BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]

    2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...

  7. Bzoj 2733: [HNOI2012]永无乡(线段树+启发式合并)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己 ...

  8. bzoj2733: [HNOI2012]永无乡 线段树合并

    永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达另一个岛. ...

  9. 【bzoj2733】[HNOI2012]永无乡 线段树合并

    Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...

随机推荐

  1. Python--day01(计算机基础)

    Python: python 是一门面向后台的编程语言,在大数据,数据分析,机器学习,人工智能,爬虫,自动化运维,web等方面具有强大功能. 基础阶段学习内容:基本语法,文件处理,函数,模块,面向对象 ...

  2. 一段c++代码实现睡眠功能

    #ifdef ACL_UNIX struct timeval tv; tv.tv_sec = delay / 1000; tv.tv_usec = (suseconds_t) (delay - tv. ...

  3. 【学习总结】GirlsInAI ML-diary day-8-list列表

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day8 认识list列表 新的数据类型:list. list是一种有序的集合,可以随时添加和删除其中的元素(链表??) 1 ...

  4. 记一次innobackupex备份恢复数据库过程

    简介:以前备份都是通过mysqldump备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易.凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗费的时间也 ...

  5. 动态库 Framework

    framework的建立和生成 都比较简单.重点会放在第三块上面(指令集说明及合并) 1.framework target建立 1.1. command + shift + N 选取 ios -> ...

  6. Codeforces Round #530 (Div. 2) C D

    C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...

  7. pta寒假作业2

    题目二币值转换 题目代码 #include<stdio.h> int main (void) { int n, initial_n; scanf("%d", & ...

  8. sublime text 批量删除空白行

    CTRL+H打开replace功能,勾选上左侧的regular expression,并填写 find what栏 : \s+$  (正则表达式)replace with栏 : (这行留空) 接着点r ...

  9. androidstudio上传代码到git上

    1.首先通过git --bare init 在服务端创建好了一个git仓库:假设git仓库在服务端的地址为:/user/lyh/project/test.git 2.androidstudio上点击V ...

  10. Python动态语言的特性

    一.动态语言相关概念 1.1 动态语言 在运行时代码可以根据某些条件改变自身结构 可以在运行时引进新的函数.对象.甚至代码,可以删除已有的函数等其他结构上的变化 常见的动态语言:Object-C.C# ...