蒟蒻写的配对堆
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e6+10;
struct Hp {
int v,id;
Hp *ch,*xd;
} node[MAXN];
Hp* merge(Hp* a,Hp* b) {
if(a->v==0)return a;
if(b->v==0)return b;
if(a->v>b->v||(a->v==b->v&&a>b))swap(a,b);
b->xd=a->ch;
a->ch=b;
return a;
}
Hp* merges(Hp* c) {
if(c->v==0||c->xd->v==0)return c;
Hp* a=c->xd,*b=a->xd;
c->xd=a->xd=node;
return merge(merge(c,a),merges(b));
}
Hp* delmin(Hp* x) {
printf("%d\n",x->v-1);
x->v=-1;
return merges(x->ch);
}
int f[MAXN];
inline int find(int x) {
return x==f[x]?x:f[x]=find(f[x]);
}
bool Union(int x,int y) {
if(x==y)return false;
f[y]=x;
return true;
}
Hp* hp[MAXN];
bool b[MAXN];
int n,m;
int main() {
scanf("%d",&n);
node[0].v=0;
for(int i=1; i<=n; ++i) {
f[i]=i,b[i]=true;
scanf("%d",&node[i].v);
node[i].v++;
node[i].id=i;
node[i].ch=node[i].xd=node;
hp[i]=&node[i];
}
scanf("%d",&m);
for(int i=1,x,y; i<=m; ++i) {
char opt;cin>>opt;
scanf("%d",&x);
if(opt=='M') {
scanf("%d",&y);
int fx=find(x),fy=find(y);
if(!b[x]||!b[y]||!Union(fx,fy))continue;
hp[fx]=merge(hp[fx],hp[fy]);
continue;
}
int fx=find(x);
if(!b[x]) {
puts("0");
continue;
}
b[hp[fx]->id]=false;
hp[fx]=delmin(hp[fx]);
}
return 0;
}