Cod sursa(job #253543)

Utilizator runnaway90Oprescu Radu Constantin runnaway90 Data 5 februarie 2009 22:11:55
Problema Heapuri Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 1.26 kb
#include<stdio.h>
#define N 200010

int a[N], h[N], pos[N], lg, nr;

void push(int x){
int aux;
        while (x/2 && a[h[x]] < a[h[x/2]]){
            aux = h[x]; h[x] = h[x/2]; h[x/2] = aux;

            pos[h[x]]=x; pos[h[x/2]]=x/2;
            x/=2;
        }
}

void pop(int x){
int y = 0, aux;

        while (x!=y){
            y = x;
            if (y*2 <= lg && a[h[x]] > a[h[y*2]]) x = y*2;
            else
            if (y*2+1 <= lg && a[h[x]] > a[h[y*2+1]])  x = y*2+1;

            aux=h[x]; h[x] = h[y]; h[y] = aux;

            pos[h[x]] = x; pos[h[y]] = y;
        }
}




int main(){
int t, c, x, i;

    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d ", &t);
    lg = 0;
    for ( ; t; t--){
        scanf("%d", &c);
        if (c <= 2) scanf("%d", &x);

        if (c == 1){
            nr++; lg++;
            a[nr] = x; pos[nr]=lg;
            h[lg] = nr;
            push(lg);
        }
        else
            if (c==2){
                a[x] = -1; push(pos[x]);
                pos[h[1]]=0;
                h[1]=h[lg--]; pos[h[1]]=1;
                if (lg>1) pop(1);
            }
            else
                printf("%d\n",a[h[1]]);
    }
    return 0;
}