Cod sursa(job #1233951)

Utilizator vtt271Vasile Toncu vtt271 Data 26 septembrie 2014 14:02:56
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2 kb
#include <fstream>

using namespace std;

long  poz[200005], index = 0, A[200005];

class MIN_HEAP{
public:
    long  H[200005];
    int N;

    void create(){ N = 0; }

    int get_min(){ return H[1]; }

    int father(int nod){ return nod/2; }
    int left_son(int nod){ return 2*nod; }
    int right_son(int nod){ return 2*nod+1; }

    void coboara(int nod){
        if( left_son(nod) > N ) return;
        if( right_son(nod) > N ){
            if( H[nod] > H[left_son(nod)] ){
                swap( H[nod], H[left_son(nod)] );
                swap(A[H[left_son(nod)]], A[H[nod]]);
            }
            return;
        }
        int next_nod = H[left_son(nod) ] < H[right_son(nod)] ? left_son(nod) : right_son(nod);
        if( H[nod] > H[next_nod] ){
            swap(H[nod], H[next_nod]);
            swap(A[H[next_nod]], A[H[nod]]);
            coboara(next_nod);
        }
    }

    void urca(int nod){
        if( nod == 1 ) return;
        if( H[father(nod)] > H[nod] ){
            swap( H[father(nod)], H[nod] );
            swap(A[H[father(nod)]], A[H[nod]]);
            urca(father(nod));
        }
    }

    void insert(long val){
        index++;
        poz[index] = val;
        if( N == 0 ) { H[++N] = val; A[val] = 1; }
        else{
            N++;
            H[N] = val;
            A[val] = N;
            urca(N);
        }
    }

    void del(int nod){
        if( nod == N) N--;
        else{
            swap(H[N], H[nod]);
            swap(A[H[N]], A[H[nod]]);
            N--;
            coboara(nod);
        }
    }

};

int main()
{
    ifstream inFile("heapuri.in");
    ofstream outFile("heapuri.out");

    int n;
    inFile >> n;

    MIN_HEAP H;
    H.create();

    long  cod, x;
    while(n--){
        inFile >> cod;
        if( cod == 3 ) outFile << H.get_min() << "\n";
        else{
            inFile >> x;
            if( cod == 1 ) H.insert(x);
            else H.del(A[poz[x]]);
        }
    }
}