# 【bzoj1455】罗马游戏 可并堆+并查集

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

10
100
0
66

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int f[1000010] , l[1000010] , r[1000010] , v[1000010] , d[1000010] , die[1000010];
char str[5];
int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
}
int merge(int x , int y)
{
if(!x) return y;
if(!y) return x;
if(v[x] > v[y]) swap(x , y);
r[x] = merge(r[x] , y);
if(d[l[x]] < d[r[x]]) swap(l[x] , r[x]);
d[x] = d[r[x]] + 1;
return x;
}
int main()
{
int n , i , m , x , y , tx , ty;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &v[i]) , f[i] = i , d[i] = 1;
scanf("%d" , &m);
while(m -- )
{
scanf("%s%d" , str , &x);
if(str[0] == 'M')
{
scanf("%d" , &y);
if(!die[x] && !die[y])
{
tx = find(x) , ty = find(y);
if(tx != ty) f[tx] = f[ty] = merge(tx , ty);
}
}
else
{
if(die[x]) printf("0\n");
else
{
tx = find(x);
die[tx] = 1;
printf("%d\n" , v[tx]);
ty = merge(l[tx] , r[tx]);
f[tx] = f[ty] = ty;
}
}
}
return 0;
}

|转载请注明 [原文链接][作者] ，谢谢！

posted @ 2017-03-10 18:59  GXZlegend  阅读(261)  评论(0编辑  收藏  举报