Cod sursa(job #1956903)

Utilizator DobosDobos Paul Dobos Data 7 aprilie 2017 09:50:18
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>
#define NMAX 200005
using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

class Heap {
    protected:
    int H[NMAX],P[NMAX],V[NMAX],N,CN;

    public:
    void UpHeap(int);
    void DownHeap(int);
    void Push(int);
    void Pop(int);
    int Top();


};

Heap Q;

void Heap :: UpHeap(int p){

    if(p > 1 && V[H[p]] < V[H[p >> 1]]){
        swap(H[p],H[p >> 1]);
        P[H[p]] = p;
        P[H[p >> 1]] = p >> 1;
        UpHeap(p >> 1);

    }
}

void Heap :: DownHeap(int p){

    int son = p << 1;
    if(son <= CN && V[H[p]] > V[H[son]]){
        swap(H[p],H[son]);
        P[H[p]] = p;
        P[H[son]] = son;
        DownHeap(son);
    }
    son = (p << 1) + 1;
    if(son <= CN && V[H[p]] > V[H[son]]){
        swap(H[p],H[son]);
        P[H[p]] = p;
        P[H[son]] = son;
        DownHeap(son);
    }

}

void Heap :: Push(int x){

    V[++N] = x;
    H[++CN] = N;
    P[N] = CN;

    UpHeap(CN);

}
void Heap :: Pop(int p){

    int poz = P[p];
    P[H[CN]] = poz;
    swap(H[poz],H[CN]);
    CN--;
    UpHeap(poz);
    DownHeap(poz);

}

int Heap :: Top(){
    return V[H[1]];
}


int main()
{
    int n,t,x;

    fin >> n;

    while(n--){
        fin >> t;

        switch(t){
            case 1:{
                fin >> x;
                Q.Push(x);
                break;
            }
            case 2:{
                fin >> x;


                Q.Pop(x);
                break;
            }
            case 3:{
                fout << Q.Top() << "\n";
            //    fout << t << " ";
                break;
            }

        }

    }

    return 0;
}