这个代码 AC 了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
int fa[1000005];
int val[1000005];
int lc[1000005];
int rc[1000005];
int dis[1000005];
int merge(int x, int y){
if(x == 0 || y == 0){
return x + y;
}
if(val[x] > val[y] || (val[x] == val[y] && x > y)){
swap(x, y);
}
rc[x] = merge(rc[x], y);
fa[rc[x]] = x;
if(dis[lc[x]] < lc[rc[x]]){
swap(lc[x], rc[x]);
}
dis[x] = dis[rc[x]] + 1;
return x;
}
void pop(int x){
val[x] = -1;
fa[lc[x]] = lc[x];
fa[rc[x]] = rc[x];
fa[x] = merge(lc[x], rc[x]);
}
int find(int x){
if(fa[x] == x){
return x;
}
fa[x] = find(fa[x]);
return fa[x];
}
int main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> val[i];
fa[i] = i;
}
dis[0] = -1;
for(int i = 0; i < m; i++){
int t, u, v;
cin >> t;
if(t == 1){
cin >> u >> v;
if(val[u] == -1 || val[v] == -1){
continue;
}
int uu = find(u);
int vv = find(v);
fa[uu] = fa[vv] = merge(uu, vv);
} else {
cin >> u;
if(val[u] == -1){
cout << -1 << endl;
} else {
int t = find(u);
cout << val[t] << endl;
pop(t);
}
}
}
return 0;
}
里
int merge(int x, int y){
if(x == 0 || y == 0){
return x + y;
}
if(val[x] > val[y] || (val[x] == val[y] && x > y)){
swap(x, y);
}
rc[x] = merge(rc[x], y);
fa[rc[x]] = x;
if(dis[lc[x]] < lc[rc[x]]){
swap(lc[x], rc[x]);
}
dis[x] = dis[rc[x]] + 1;
return x;
}
里
if(dis[lc[x]] < lc[rc[x]]){
swap(lc[x], rc[x]);
}
lc[rc[x]]