#include<cstdio> 
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#define maxint (2147483647)
#define l(a) ((a)<<1)
#define r(a) ((a)<<1|1)
#define b(a) (2<<(a))
#define f(a) ((a)>>1)
#define rep(i,a,b) for(int i=a;i<=(b);i++)
#define clr(a) memset(a,0,sizeof(a))
typedef long long ll;
using namespace std;
int readint(){
    int t=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(isdigit(c)){
        t=(t<<3)+(t<<1)+c-'0';
        c=getchar();
    }
    return t*f;
}
ll readll(){
    ll t=0ll,f=1ll;char c=getchar();
    while(!isdigit(c)){
        if(c=='-') f=-1ll;
        c=getchar();
    }
    while(isdigit(c)){
        t=(t<<3ll)+(t<<1ll)+ll(c-'0');
        c=getchar();
    }
    return t*f;
}
const int maxn=1000009;
struct heap{
    int cnt,a[maxn<<2];
    inline void init(){
        clr(a);cnt=0;
    }
    inline int top(){
        return a[1];
    }
    inline void up(int x){
        if(x==1) return;
        if(a[x]<a[f(x)]){
            swap(a[f(x)],a[x]);
            up(f(x));
        }
    }
    inline void push(int t){
        a[++cnt]=t;up(cnt);
    }
    inline void pop(){
        if(!cnt) return;
        a[1]=a[cnt];a[cnt--]=0;down(1);
    }
    inline void down(int x){
        if(a[l(x)]+a[r(x)]==0) return;
        if(a[l(x)]*a[r(x)]==0){
            if(a[l(x)]) if(a[l(x)]<a[x]) swap(a[l(x)],a[x]),down(l(x));
            if(a[r(x)]) if(a[r(x)]<a[x]) swap(a[r(x)],a[x]),down(r(x));
            return;
        }
        if(a[x]>min(a[l(x)],a[r(x)])) if(a[l(x)]<a[r(x)]){
            swap(a[l(x)],a[x]);down(l(x));
        }else{
            swap(a[r(x)],a[x]);down(r(x));
        }
    }
    inline void out(){
        rep(i,1,cnt) printf("  %d %d\n",i,a[i]);
    }
}Q;
int n; 
int main(){
    //freopen("#intput.txt","r",stdin);
    //freopen("#output.txt","w",stdout);
    n=readint();Q.init();
    while(n--){
        int opt=readint();
        if(opt==1){
            int t=readint();Q.push(t);
        }else if(opt==2) printf("%d\n",Q.top());
        else Q.pop();
    }     
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
二叉堆

 

posted @ 2017-10-30 19:35  ChenThree  阅读(126)  评论(0编辑  收藏  举报