二叉堆模板

题目

测试题目传送门

Code

//By zuiyumeng
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Re register
#define Ms(a,b) memset((a),(b),sizeof(a))
#define Fo(i,a,b) for(Re int i=(a),_=(b);i<=_;i++)
#define Ro(i,a,b) for(Re int i=(b),_=(a);i>=_;i--)
using namespace std;

inline int read() { 
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
    while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
    return x*f;
}

const int N=1e6+10;
int tot;
int hep[N];

void insert(int x) {
    hep[++tot]=x; int now=tot;
    while((now>>1) && hep[now>>1]>hep[now]) 
        swap(hep[now>>1],hep[now]),now>>=1;
}

void deleteMin() {
    hep[1]=hep[tot]; tot--;
    for(int now=1,minn;(now<<1)<=tot;now=minn) {
        int ls=now<<1,rs=(now<<1)+1;
        if(hep[ls]<hep[rs]) minn=ls; else minn=rs;
        if(hep[now]>hep[minn]) swap(hep[now],hep[minn]);
    }
}

int main() {
    int n=read();
    while(n--) {
        int opt=read();
        if(opt==1) insert(read());
        else if(opt==2) cout<<hep[1]<<endl;
        else deleteMin();
    }
    return 0;
}
posted @ 2020-08-22 15:22  醉语梦  阅读(107)  评论(0编辑  收藏  举报