Cod sursa(job #1458459)

Utilizator cristina_borzaCristina Borza cristina_borza Data 7 iulie 2015 16:02:02
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#define NMAX 1000000
#define VMAX 2000000000

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n , tip , heap[NMAX] , pi[NMAX] , ph[NMAX] , sf , nr ;

void heapup(int p);
void heapdown(int p);

int main()
{
    int x ;
    f >> n ;
    for(int i = 1 ; i <= n ; ++i){
        heap[i] = VMAX;
    }
    for(int i = 1 ; i <= n ; ++i){
        f >> tip ;
        if(tip == 1){
            f >> x ;
            heap[++sf] = x ;
            pi[++nr] = sf ;
            ph[sf] = nr ;
            heapup(sf) ;
        }
        if(tip == 2){
            f >> x ;
            --sf ;
            heap[pi[x]] = VMAX ;
            heapdown(pi[x]) ;
        }
        if(tip == 3){
            if(heap[1] != 82){
                ++x;
            }
            g << heap[1] << '\n' ;
        }
    }
    return 0;
}

void heapup(int p){
    while(heap[p / 2] > heap[p] && p > 1){
        swap(heap[p] , heap[p / 2]);
        pi[nr] = p / 2 ;
        pi[ph[p / 2]] = p ;
        ph[p] = ph[p / 2] ;
        ph[p / 2] = nr ;
        p = p / 2;
    }
}

void heapdown(int p){
    while(heap[p] > min(heap[p * 2] , heap[p * 2 + 1]) && p < sf){
        if(heap[p * 2] < heap[p * 2 + 1]){
            swap(heap[p] , heap[p * 2]);
            pi[ph[p * 2]] = p ;
            ph[p] = ph[p * 2];
            p = p * 2 ;
        }
        else{
            swap(heap[p] , heap[p * 2 + 1]);
            pi[ph[p * 2 + 1]] = p ;
            ph[p] = ph[p * 2 + 1];
            p = p * 2 + 1 ;
        }
    }
}