题目
描述
永无乡包含 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 个数,依次描述从岛 1 到岛 n 的重要度排名。随后的 m 行每行是用空格隔开的两个正整数 ai 和 bi,表示一开始就存 在一座连接岛 ai 和岛 bi 的桥。后面剩下的部分描述操作,该部分的第一行是一个正整数 q, 表示一共有 q 个操作,接下来的 q 行依次描述每个操作,操作的格式如上所述,以大写字母 Q 或B 开始,后面跟两个不超过 n 的正整数,字母与数字以及两个数字之间用空格隔开。
对于 20%的数据 n<=1000,q<=1000
对于 100%的数据 n<=100000,m<=n,q<=300000
输出
对于每个 Q x k 操作都要依次输出一行,其中包含一个整数,表 示所询问岛屿的编号。如果该岛屿不存在,则输出-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
解题思路
题目只有两个操作:合并、找第k小的数,可以考虑用平衡树来实现:
- 平衡树按重要度由小到大的顺序建树
- 找第k小的数只用在相应平衡树上二分着找就行了
- 合并两颗平衡树需要用所谓的启发式合并:把较小的平衡树中每个元素暴力地一个一个插入到较大的平衡树里去即可,由于每次暴力插入平衡树大小最多翻倍,故最多进行 logn l o g n 次这样的操作,所以总的合并时间复杂度是 O(nlogn) O ( n l o g n ) 的。启发式合并可谓是一种优美的暴力
稍微麻烦点的地方是怎么找到一座岛所在的平衡树的根的编号,也许就是这儿让我的代码跑得有点慢……
Code
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 100005;
int n, m, val[N], ai, bi, q, tval[N], tnum[N], tx, f[N<<3];
char opt[2];
struct Splay{
int size, son[2], fa, val, num;
Splay(){ size = son[0] = son[1] = fa = 0; }
}tr[N<<3];
struct OPT_Splay{
int cnt;
inline void pushup(int id){
tr[id].size = tr[tr[id].son[0]].size + tr[tr[id].son[1]].size + 1;
}
inline int which(int x){ return tr[tr[x].fa].son[1] == x; }
inline void rotate(int x, int kind){
int y = tr[x].fa, z = tr[y].fa, B = tr[x].son[kind];
tr[x].son[kind] = y, tr[y].son[!kind] = B, tr[z].son[which(y)] = x;
tr[x].fa = z, tr[y].fa = x, tr[B].fa = y;
pushup(y), pushup(x);
}
inline void splay(int &rt, int x, int goal){
if(x == goal) return;
while(tr[x].fa != goal){
int y = tr[x].fa, z = tr[y].fa;
int dir1 = !which(x), dir2 = !which(y);
if(z == goal) rotate(x, dir1);
else{
if(dir1 == dir2) rotate(y, dir2);
else rotate(x, dir1);
rotate(x, dir2);
}
}
if(goal == 0) rt = x;
}
inline int select(int rt, int x){
int now = rt;
while(tr[now].val != x){
if(tr[now].val < x) now = tr[now].son[1];
if(tr[now].val > x) now = tr[now].son[0];
}
return now;
}
inline int getRoot(int x){
while(tr[x].fa != 0) x = tr[x].fa;
return x;
}
inline void getSeries(int x){
if(tr[x].son[0]) getSeries(tr[x].son[0]);
tval[++tx] = tr[x].val, tnum[tx] = tr[x].num;
if(tr[x].son[1]) getSeries(tr[x].son[1]);
}
inline void combine(int a, int b){
if(a == b) return;
if(tr[a].size > tr[b].size) swap(a, b);
tx = 0, getSeries(a);
for(int i = 1; i <= tx; i++){
int now = b, pnow = 0, pdir;
while(now){
if(tr[now].val > tval[i]) pnow = now, now = tr[now].son[0], pdir = 0;
else if(tr[now].val < tval[i]) pnow = now, now = tr[now].son[1], pdir = 1;
}
tr[++cnt].val = tval[i], tr[cnt].num = tnum[i], tr[cnt].size = 1, tr[cnt].fa = pnow, f[tnum[i]] = cnt;
tr[pnow].son[pdir] = cnt;
pushup(pnow);
splay(b, select(b, tval[i]), 0);
}
}
inline int query(int a, int k){
int now = a;
if(tr[now].size < k) return -1;
while(tr[tr[now].son[0]].size + 1 != k){
if(tr[tr[now].son[0]].size >= k) now = tr[now].son[0];
else k -= tr[tr[now].son[0]].size + 1, now = tr[now].son[1];
}
return tr[now].num;
}
}BST;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%d", &tr[++BST.cnt].val);
tr[BST.cnt].num = i, tr[BST.cnt].size = 1, f[i] = BST.cnt;
}
while(m--){
scanf("%d%d", &ai, &bi);
BST.combine(BST.getRoot(f[ai]), BST.getRoot(f[bi]));
}
scanf("%d", &q);
while(q--){
scanf("%s", opt);
scanf("%d%d", &ai, &bi);
if(opt[0] == 'B') BST.combine(BST.getRoot(f[ai]), BST.getRoot(f[bi]));
else if(opt[0] == 'Q') printf("%d\n", BST.query(BST.getRoot(f[ai]), bi));
}
return 0;
}