Cod sursa(job #632731)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 12 noiembrie 2011 10:49:39
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <stdio.h>
int n,h[200001],a[200001],p[200001],k,x;

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

void insert(int v){
    int n,f;
    x++; //for a
    k++; //for h
    h[k]=v;
    a[x]=k;
    p[k]=x;
    f=k; n=f/2;
    while(n!=0&&h[f]<h[n]){
        swap(h[f],h[n]);
        swap(a[p[f]],a[p[n]]);
        swap(p[f],p[n]);
        f=n; n=f/2; }
}

void remove(int v){
    int n,f;
    h[a[v]]=h[k];
    p[v]=p[k];
    a[p[k]]=v;
    n=v; f=n*2; if(f+1<=n&&h[f+1]<h[f])f++;
    while(f<=n&&h[n]>h[f]){
        swap(h[f],h[n]);
        swap(a[p[f]],a[p[n]]);
        swap(p[f],p[n]);
        n=f; f=n*2; if(f+1<=n&&h[f+1]<h[f])f++; }
}


int main(){
    int 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); insert(x); } break;
            case 2: {scanf("%d",&x); remove(x); } break;
            case 3: printf("%d\n",h[1]); } }
}