Cod sursa(job #2670805)

Utilizator bubblegumixUdrea Robert bubblegumix Data 10 noiembrie 2020 18:33:08
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int LIM = 200006;
int m;
int n;
int al_catelea;
int Heap[LIM];
int Val[LIM];
int Poz[LIM];
ifstream f("heapuri.in");
ofstream g("heapuri.out");

inline void Swap(int x, int y)
{
    swap(Heap[x], Heap[y]);
    swap(Poz[Heap[x]], Poz[Heap[y]]);
}


void UpHeap(int nod)
{
    int Father = nod / 2;
    if (Father && Val[Heap[nod]] < Val[Heap[Father]])
    {
        Swap(nod, Father);
        UpHeap(Father);
    }

}
void DownHeap(int nod)
{
    int son = nod * 2;
    if (son + 1 <= n && Val[Heap[son]] > Val[Heap[son + 1]])
        son++;
    if (son <= n && Val[Heap[son]] < Val[Heap[nod]])
    {
        Swap(son, nod);
        DownHeap(son);
    }
}

void heap_pop(int x)
{
    Swap(x, n);
    n--;
    DownHeap(x);
}

void heap_push(int x)
{  
    al_catelea++;
    n++;
    Val[al_catelea] = x;
    Heap[n] = al_catelea;
    Poz[al_catelea] = n;
    UpHeap(n);

}

int main()
{
    int op;
    int x;
    f >> m;
    for (int i = 1; i <= m; i++)
    {
        f >> op;
        if (op == 1)
        {
            f >> x;
            heap_push(x);
        }
        else
            if (op == 2)
            {
                f >> x;
                heap_pop(Poz[x]);
            }
            else
                g << Val[Heap[1]] << endl;
       

    }
   
    
    f.close();
    g.close();
    return 0;


}