9
15
2015
0

1455: 罗马游戏

1455: 罗马游戏

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 875  Solved: 352
[Submit][Status][Discuss]

Description

罗马皇帝很喜欢玩杀人游戏。 他的军队里面有n个人,每个人都是一个独立的团。最近举行了一次平面几何测试,每个人都得到了一个分数。 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻。他决定玩这样一个游戏。 它可以发两种命令: 1. Merger(i, j)。把i所在的团和j所在的团合并成一个团。如果i, j有一个人是死人,那么就忽略该命令。 2. Kill(i)。把i所在的团里面得分最低的人杀死。如果i这个人已经死了,这条命令就忽略。 皇帝希望他每发布一条kill命令,下面的将军就把被杀的人的分数报上来。(如果这条命令被忽略,那么就报0分)

Input

第一行一个整数n(1<=n<=1000000)。n表示士兵数,m表示总命令数。 第二行n个整数,其中第i个数表示编号为i的士兵的分数。(分数都是[0..10000]之间的整数) 第三行一个整数m(1<=m<=100000) 第3+i行描述第i条命令。命令为如下两种形式: 1. M i j 2. K i

Output

如果命令是Kill,对应的请输出被杀人的分数。(如果这个人不存在,就输出0)

Sample Input

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

 

HINT

 

Source

    看到这道题我才发现我做题的顺序错了。。。这才是最最简单的可并堆模板。。。题解应该也不需要吧。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
int l[N],r[N],f[N],die[N],key[N],n,m,x,y;
char opt[10];
int read(){
	int x = 0,f = 1,c = getchar();
	while (c<'0' || c>'9') {if (c=='-') f = -1;c = getchar();}
	while (c>='0' && c<='9') x = x*10+c-'0',c = getchar();
	return x*f;
}
int merge(int a,int b){
	if (!a || !b) return a+b;
	if (key[a]>key[b]) swap(a,b);
	int res = r[a] = merge(r[a],b);
	f[res] = a;swap(r[a],l[a]);
	return a;
}
int get(int x){while (f[x]) x = f[x];return x;}
int main(){
	n = read();
	for (int i=1;i<=n;i++) key[i] = read();
	m = read();
	for (int i=1;i<=m;i++){
		scanf("%s",opt);
		if (opt[0]=='M'){
			x = read();y = read();
			if (die[x] || die[y]) continue;
			int fx = get(x),fy = get(y);
			if (fx!=fy) merge(fx,fy);
		}
		else {
			x = read();
			if (die[x]){printf("0\n");continue;}
			int top = get(x);die[top] = 1;
			int New = merge(l[top],r[top]);f[New] = 0;
			printf("%d\n",key[top]);
		}
	}
}
Category: BZOJ题解 | Tags: | Read Count: 304

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com