Cod sursa(job #632757)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 12 noiembrie 2011 11:41:00
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <stdio.h>
#define MAX 200001
int h[MAX],a[MAX],p[MAX],n,m;

void swap(int&x,int&y){int z=x;x=y;y=z;}

void push(int x){
    int t,f;
    n++;m++;
    h[n]=x;
    p[m]=n;
    a[n]=m;
    f=n; t=f/2;
    while(t!=0&&h[f]<h[t]){
        swap(h[t],h[f]);
        p[a[t]]=f;
        p[a[f]]=t;
        swap(a[t],a[f]);
        f=t; t=f/2; }
}

void pop(int x){
    int t,f;
    h[p[x]]=h[n];
    p[h[n]]=p[x];
    a[p[x]]=a[n];
    n--;
    t=p[x]; f=t*2; if(f+1<=n&&h[f+1]<h[f])f++;
    while(f<=n&&h[t]>h[f]){
        swap(h[t],h[f]);
        p[a[t]]=f;
        p[a[f]]=t;
        swap(a[t],a[f]);
        t=f; f=t*2; if(f+1<=n&&h[f+1]<h[f])f++;}
}

int main(){
    int n,i,o,x;
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%d",&o);
            switch(o){
                case 1: {scanf("%d",&x); push(x);} break;
                case 2: {scanf("%d",&x); pop(x); } break;
                case 3: printf("%d\n",h[1]); } }
}