写一个大根堆就可以了 , 注意并查集找fa不能路径压缩 , 因为每个点所在的集合的根是变化的
#include<bits/stdc++.h>
#define N 1000050
using namespace std;
struct Node{int l,r;}t[N];
int n,m,a[N],fa[N],killed[N]; char s[10];
int read(){
int cnt=0;char ch=0;
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt;
}
int find(int x){return x==fa[x]?x:find(fa[x]);}
int Merge(int x,int y){
if(!x || !y) return x+y;
if(a[x] > a[y]) swap(x,y);
t[x].l = Merge(t[x].l , y);
fa[t[x].l] = x;
if(rand()%2) swap(t[x].l , t[x].r);
return x;
}
void Delete(int x){
killed[x]=1;
fa[t[x].l] = t[x].l;
fa[t[x].r] = t[x].r;
Merge(t[x].l , t[x].r);
}
void Kill(int x){
if(killed[x]){printf("0\n"); return;}
x=find(x); printf("%d\n",a[x]); Delete(x);
}
int main(){
srand(time(0));
n=read();
for(int i=1;i<=n;i++) a[i]=read() , fa[i]=i;
m=read(); while(m--){
scanf("%s",s);
if(s[0]=='K') Kill(read());
if(s[0]=='M'){
int x=read(),y=read();
if(killed[x] || killed[y]) continue;
x=find(x),y=find(y);
if(x != y) Merge(x,y);
}
}return 0;
}