BZOJ1455罗马游戏

 

 

 

 

左偏树裸题

(搞板子搞到绝望x

//罗马游戏 
#include <bits/stdc++.h>
using namespace std;
const int N = 1000050;
int val[N], l[N], r[N], d[N], fa[N];
int x, y, n, m;
char ord;
// 合并以 x, y为根的堆,返回合并后的堆顶
int merge(int x, int y) 
{
    if(!x||!y) return x|y;
// 键值小的作为堆顶
    if (val[x] > val[y])    swap(x, y);
    r[x] = merge(r[x], y);
// 维护左偏性质
    if (d[r[x]] > d[l[x]])
        swap(l[x], r[x]);
// 重新计算距离
    d[x] = d[r[x]] + 1;
    return x;
}

int find(int x) 
{
  if(fa[x] == x) return x;
  else return fa[x] = find(fa[x]);
 }

int main() 
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &val[i]);
        fa[i] = i;
    }

    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        cin >> ord;
        if (ord == 'M') {
            scanf("%d%d", &x, &y);
            if (val[x] < 0 || val[y] < 0) continue;
            x = find(x), y = find(y);
            if (x != y) {
                int t = merge(x, y);
                fa[x] = fa[y] = t;
            }
        } 
        else {
            scanf("%d", &x);
            if (val[x] < 0) {
                puts("0");
                continue;
            }
            x = find(x);
            printf("%d\n", val[x]);
            val[x] = -1;
            fa[x] = merge(l[x], r[x]);
            fa[fa[x]] = fa[x];
        }
    }
    return 0;
}

 

满堂花醉三千客,一剑霜寒十四州
posted @ 2019-10-03 20:59  QUEKI嶺冬  阅读(149)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */