裸的可并堆 Random Heap 还是很兹瓷啊
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(char &x){
for (x=nc();x!='K' && x!='M';x=nc());
}
const int N=1000005;
inline int ran() { static int x=31253125;x+=(x<<4)+1;return x&65536; }
struct node{node *l,*r;int key,idx;}nodes[N];
node *M(node *p,node *q) {
return (!p||!q)?(p?p:q):(p->key>q->key?M(q,p):((ran()?p->l=M(p->l,q):p->r=M(p->r,q)),p));
}
int n,Q;
int fat[N],killed[N];
node *root[N];
int Fat(int u) { return u==fat[u]?u:fat[u]=Fat(fat[u]); }
int main()
{
int s,x,y,fx,fy; char order;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n);
for (int i=1;i<=n;i++)
read(s),nodes[i].key=s,nodes[i].l=nodes[i].r=NULL,nodes[i].idx=i,root[i]=nodes+i,fat[i]=i;
read(Q);
while (Q--)
{
read(order);
if (order=='M')
{
read(x); read(y); if (killed[x] || killed[y]) continue;
fx=Fat(x); fy=Fat(y);
if (fx!=fy)
{
root[fx]=M(root[fx],root[fy]);
fat[fy]=fx;
}
}
else
{
read(x); if (killed[x]) { printf("0\n"); continue; }
fx=Fat(x);
printf("%d\n",root[fx]->key);
killed[root[fx]->idx]=1; root[fx]=M(root[fx]->l,root[fx]->r);
}
}
return 0;
}