【题目来源】
https://www.luogu.com.cn/problem/P3224
【题目描述】
永无乡包含 n 座岛,编号从 1 到 n ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以 到达岛 b ,则称岛 a 和岛 b 是连通的。
现在有两种操作:
B x y 表示在岛 x 与岛 y 之间修建一座新桥。
Q x k 表示询问当前与岛 x 连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪座,请你输出那个岛的编号。
【输入格式】
第一行是用空格隔开的两个整数,分别表示岛的个数 n 以及一开始存在的桥数 m。
第二行有 n 个整数,第 i 个整数表示编号为 i 的岛屿的排名 pi。
接下来 m 行,每行两个整数 u,v,表示一开始存在一座连接编号为 u 的岛屿和编号为 v 的岛屿的桥。
接下来一行有一个整数,表示操作个数 q。
接下来 q 行,每行描述一个操作。每行首先有一个字符 op,表示操作类型,然后有两个整数 x,y。
(1)若 op 为 Q,则表示询问所有与岛 x 连通的岛中重要度排名第 y 小的岛是哪座,请你输出那个岛的编号。
(2)若 op 为 B,则表示在岛 x 与岛 y 之间修建一座新桥。
【输出格式】
对于每个询问操作都要依次输出一行一个整数,表示所询问岛屿的编号。如果该岛屿不存在,则输出 −1 。
【输入样例】
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
【输出样例】
-1
2
5
1
2
【数据范围】
对于 20% 的数据,保证 n≤10^3, q≤10^3。
对于 100% 的数据,保证 1≤m≤n≤10^5, 1≤q≤3×10^5,p 为一个 1∼n 的排列,op∈{Q,B},1≤u,v,x,y≤n。
【算法分析】
Splay 树简介及代码模板:https://blog.csdn.net/hnjzsyjyj/article/details/138504578
【算法代码】
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
struct Node {
int ch[2],fa,val;
int id;
int cnt; //Number of repeated nodes
int sz; //Number of elements in the subtree
} tr[N];
int tot=0; //Total number of newly added nodes
int root[N],pre[N];
int cur_rt,tmp_id;
bool which(int u) {
return tr[tr[u].fa].ch[1]==u;
}
void pushup(int u) {
tr[u].sz=tr[tr[u].ch[0]].sz+tr[tr[u].ch[1]].sz+tr[u].cnt;
}
void rotate(int u) {
int y=tr[u].fa;
int z=tr[y].fa;
int k=which(u); //k:Direction flag,0 for lson,1 for rson
int w=tr[u].ch[k^1];
if(z) tr[z].ch[which(y)]=u;
tr[u].fa=z;
tr[u].ch[k^1]=y;
tr[y].fa=u;
tr[y].ch[k]=w;
if(w) tr[w].fa=y;
pushup(y);
pushup(u);
}
void splay(int u,int goal) {
while(tr[u].fa!=goal) {
int y=tr[u].fa;
int z=tr[y].fa;
if(z!=goal) {
rotate(which(y)==which(u)?y:u);
}
rotate(u);
}
if(!goal) root[cur_rt]=u;
}
void insert(int v) {
int p=root[cur_rt],f=0,d=0;
while(p) {
if(tr[p].val==v) {
tr[p].cnt++;
splay(p,0);
pushup(root[cur_rt]);
return;
}
f=p;
d=(v>tr[p].val);
p=tr[p].ch[d];
}
tot++;
tr[tot]= {{0,0},f,v,tmp_id,1,1};
if(f) tr[f].ch[d]=tot;
splay(tot,0);
}
int kth(int k) {
int p=root[cur_rt];
while(p) {
int ls=tr[tr[p].ch[0]].sz;
if(k<=ls) {
p=tr[p].ch[0];
} else if(k<=ls+tr[p].cnt) {
splay(p,0);
return tr[p].id;
} else {
k-=ls+tr[p].cnt;
p=tr[p].ch[1];
}
}
return -1;
}
void dfs(int u) {
if(!u) return;
dfs(tr[u].ch[0]);
tmp_id=tr[u].id;
insert(tr[u].val);
dfs(tr[u].ch[1]);
}
int find(int x) {
if(x!=pre[x]) pre[x]=find(pre[x]);
return pre[x];
}
void merge(int x,int y) {
x=find(x);
y=find(y);
if(x==y) return;
if(tr[root[x]].sz>tr[root[y]].sz) swap(x,y);
pre[x]=y;
cur_rt=y;
dfs(root[x]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++) {
pre[i]=i;
int p;
cin>>p;
cur_rt=i;
tot++;
tr[tot]= {{0,0},0,p,i,1,1};
root[i]=tot;
}
while(m--) {
int u,v;
cin>>u>>v;
merge(u,v);
}
int T;
cin>>T;
while(T--) {
char op;
int x,y;
cin>>op>>x>>y;
if(op=='Q') {
int fx=find(x);
cur_rt=fx;
if(y>tr[root[fx]].sz) cout<<"-1\n";
else cout<<kth(y)<<"\n";
} else if(op=='B') merge(x,y);
}
return 0;
}
/*
in:
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
out:
-1
2
5
1
2
*/
【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/138504578
https://blog.csdn.net/hnjzsyjyj/article/details/170840207
https://mp.weixin.qq.com/s/DWJPHbUhsQ7D9Xlfy1DvdQ
472



被折叠的 条评论
为什么被折叠?



