可并堆水题

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

#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

BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )的更多相关文章

  1. BZOJ 1455: 罗马游戏 [可并堆]

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1715  Solved: 718[Submit][Status][Discuss] ...

  2. bzoj 1455: 罗马游戏 左偏树+并查集

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 668  Solved: 247[Submit][Status] Descriptio ...

  3. bzoj 1455: 罗马游戏

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MB Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最 ...

  4. BZOJ 1455 罗马游戏 左偏树

    题目大意:给定n个点,每一个点有一个权值,提供两种操作: 1.将两个点所在集合合并 2.将一个点所在集合的最小的点删除并输出权值 非常裸的可并堆 n<=100W 启示式合并不用想了 左偏树就是快 ...

  5. BZOJ 1455 罗马游戏 ——左偏树

    [题目分析] 左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆. 感觉和启发式合并基本相同. 其实并没有快很多. 本人的左偏树代码自带大常数,借鉴请慎重 [代码] #i ...

  6. 【BZOJ 1455】 1455: 罗马游戏 (可并堆-左偏树+并查集)

    1455: 罗马游戏 Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那 ...

  7. 1455: 罗马游戏[左偏树or可并堆]

    1455: 罗马游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1861  Solved: 798[Submit][Status][Discuss] ...

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

    题目描述 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻.他决定玩这样 ...

  9. BZOJ 4569 [Scoi2016]萌萌哒 | ST表 并查集

    传送门 BZOJ 4569 题解 ST表和并查集是我认为最优雅(其实是最好写--)的两个数据结构. 然鹅!他俩加一起的这道题,我却--没有做出来-- 咳咳. 正解是这样的: 类似ST表有\(\log ...

随机推荐

  1. parquet code demo

    http://www.programcreek.com/java-api-examples/index.php?source_dir=hiped2-master/src/main/java/hip/c ...

  2. ORA-12514(TNS:监听程序当前无法识别...)

    记录: ORA-12514(TNS:监听程序当前无法识别...)的解决方案 在安装ORACLE 11G 过程中由于配置的原因,安装过程中报了如下错误:   按照安装提示执行后面的操作后,打开PL/SQ ...

  3. python 冒泡和快排,不多说【无聊】

    #-*-coding:utf8-*- import random a=[] b=[] def init_array(): for i in range(10000): v = random.randi ...

  4. html 浮动元素

    在CSS布局中分为内联元素(display:inline)和块状元素(display:block),块状元素默认会占据一行,可设置高度宽度以及边距,而内联元素不会也不能设置.常见的内联元素有:a.sp ...

  5. openGL 旋转的图形 矩阵操作

    #include <windows.h> #ifdef __APPLE__ #include <GLUT/glut.h> #else #include <GL/glut. ...

  6. .net mvc RazorEngine 字符串razor参数替换

    在.net中有一个比较好的字符串参数替换的方案RazorEngine推荐大家看看原网站,然后做个小联系然后你就懂啦 首先呢得下载一个吧, vs中tools-> Library Paging Ma ...

  7. 在windows系统中安装hadoop

    1.安装Cygwin 从http://www.cygwin.com/ 下载cygwin的setup.exe,双击运行: 选择从Internet安装: 设置安装目录: 设置安装包目录: 设置“Inter ...

  8. Foundation Sorting: Shellsort

    /* Shell Sorting. * Implemention history:. * 2013-09-15, Mars Fu, first version. */ /* [Shell Sortin ...

  9. 编译和安装shibboleth-sp遇到的问题

    In file included from mod_shib_20.cpp:68: mod_shib.cpp:118: warning: deprecated conversion from stri ...

  10. windows版的node.js简单示例

    1.下载node.exe放到任意目录,假设E:\nodejs\ 2.在E:\nodejs\下新建helloworld.js,输入以下内容,保存关闭 var http = require('http') ...