BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )

可并堆水题 

---------------------------------------------------------

#include<bits/stdc++.h>
 
using namespace std;
 
const int maxn = 1000009;
 
struct Node {
Node *l, *r, *ch;
int v, id;
} pool[maxn], *null, *pt = pool, *q[maxn];
 
Node* newNode(int v, int id) {
pt->l = pt->r = pt->ch = null; pt->v = v; pt->id = id;
return pt++;
}
 
void init() {
null = pt++;
null->l = null->r = null->ch = null;
}
 
Node* join(Node* x, Node* y) {
if(x->v < y->v) swap(x, y);
x->l = y; x->r = y->ch;
y->ch->l = x; y->ch = x;
return y;
}
 
struct ph {
Node* root;
void init() {
root = null;
}
inline Node* top() {
return root;
}
inline void push(int id, int v) {
Node* t = newNode(v, id);
root = root == null ? t : join(root, t);
}
void pop() {
if(root->ch != null) {
int n = 0;
for(Node* t = root->ch; t != null; q[n++] = t, t = t->r);
int m = n >> 1;
for(int i = 0; i < m; i++) {
q[i]->l = q[i]->r = null;
q[i + m]->l = q[i + m]->r = null;
q[i] = join(q[i], q[i + m]);
}
root = q[0];
if((n & 1) && m) root = join(root, q[n - 1]);
for(int i = 1; i < m; i++)
   root = join(root, q[i]);
} else 
   root = null;
}
} S[maxn];
 
bool alive[maxn];
int fa[maxn];
 
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
 
int main() {
init();
int N; scanf("%d", &N);
for(int i = 0; i < N; i++) {
fa[i] = i;
alive[i] = true;
int mark; scanf("%d", &mark);
S[i].init();
S[i].push(i, mark);
}
int M; scanf("%d", &M);
while(M--) {
char c; scanf(" %c", &c);
if(c == 'M') {
int u, v; scanf("%d%d", &u, &v); u--; v--;
if(!alive[u] || !alive[v]) continue;
u = find(u); v = find(v);
if(u == v) 
continue;
else 
fa[u] = v;
S[v].root = join(S[u].root, S[v].root);
} else {
int t; scanf("%d", &t); t--;
if(!alive[t]) {
puts("0");
continue;
}
t = find(t);
Node* o = S[t].top(); S[t].pop();
alive[o->id] = false;
printf("%d\n", o->v);
}
}
return 0;
}

---------------------------------------------------------  

1455: 罗马游戏

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 877  Solved: 354
[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

 

posted @ 2015-09-20 11:15  JSZX11556  阅读(538)  评论(7编辑  收藏  举报