# BZOJ_1455_罗马游戏_可并堆

BZOJ_1455_罗马游戏_可并堆

1. M i j
2. K i

5
100 90 66 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4

## Sample Output

10
100
0
66

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 1000050
#define mr(x,y) make_pair(x,y)
#define fs first
#define sd second
int n,fa[N],kill[N],ls[N],rs[N],v[N],d[N];
char s[10];
int find(int x) {
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int merge(int x,int y) {
if(!x) return y;
if(!y) return x;
if(v[x]>v[y]) swap(x,y);
rs[x]=merge(rs[x],y);
if(d[ls[x]]<d[rs[x]]) swap(ls[x],rs[x]);
d[x]=d[rs[x]]+1;
return x;
}
int main() {
scanf("%d",&n);
int i,x,y;
for(i=1;i<=n;i++) {
fa[i]=i; d[i]=1;
scanf("%d",&v[i]);
}
int m;
scanf("%d",&m);
while(m--) {
scanf("%s%d",s,&x);
if(s[0]=='M') {
scanf("%d",&y);
if(kill[x]||kill[y]) continue;
int dx=find(x),dy=find(y);
if(dx!=dy) {
fa[dx]=fa[dy]=merge(dx,dy);
}
}else {
if(kill[x]) {
puts("0");
continue;
}
int dx=find(x);
kill[dx]=1;
printf("%d\n",v[dx]);
int dy=merge(ls[dx],rs[dx]);
fa[dx]=fa[dy]=dy;
}
}
}


posted @ 2018-04-20 13:38  fcwww  阅读(145)  评论(0编辑  收藏  举报