bzoj1455

只能说随机是个好东西比左偏树好写一点

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000010
using namespace std;

struct Heap{
    Heap *son[2];
    int val;

    Heap(int _):val(_) {
        son[0] = son[1] = NULL;
    }
    Heap() {}
}*heap[MAX],mempool[MAX],*C = mempool + 1;

Heap *Merge(Heap *x,Heap *y)
{
    if(x == NULL)   return y;
    if(y == NULL)   return x;
    if(x->val > y->val)    swap(x,y);
    bool k = rand()&1;
    x->son[k] = Merge(x->son[k],y);
    return x;
}

int points,asks;
int src[MAX];
bool killed[MAX];
char s[10];

int father[MAX];

int Find(int x)
{
    if(father[x] == x)  return x;
    return father[x] = Find(father[x]);
}

int main()
{
    srand(19970806);
    cin >> points;
    for(int i = 1; i <= points; ++i)
        father[i] = i;
    for(int x,i = 1; i <= points; ++i) {
        scanf("%d",&x);
        heap[i] = new (C++)Heap(x);
    }
    cin >> asks;
    for(int x,y,i = 1; i <= asks; ++i) {
        scanf("%s",s);
        if(s[0] == 'M') {
            scanf("%d%d",&x,&y);
            if(killed[x] || killed[y])  continue;
            int fx = Find(x),fy = Find(y);
            if(fx == fy)    continue;
            father[fy] = fx;
            heap[fx] = Merge(heap[fx],heap[fy]);
        }
        else {
            scanf("%d",&x);
            if(killed[x]) {
                puts("0");
                continue;
            }
            int fx = Find(x);
            printf("%d\n",heap[fx]->val);
            killed[heap[fx] - mempool] = true;
            heap[fx] = Merge(heap[fx]->son[0],heap[fx]->son[1]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值