Cod sursa(job #2670654)

Utilizator bubblegumixUdrea Robert bubblegumix Data 10 noiembrie 2020 14:01:46
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int LIM = 2000006;
int m;
int n;
int Heap[LIM];
int ordine[LIM];
int Nod[LIM];
ifstream f("heapuri.in");
ofstream g("heapuri.out");

void Cerne(int x)
{
    int fiu = 2 * x;
    if (fiu + 1 <= n && Heap[fiu + 1] < Heap[fiu])
        fiu++;

    if (fiu <= n && Heap[fiu] < Heap[x])
    {
        swap(Heap[fiu], Heap[x]);
        swap(Nod[Heap[fiu]], Nod[Heap[x]]);
        Cerne(fiu);
    }
}
void heap_pop(int x)
{
    int k = Heap[n];
    swap(Heap[Nod[ordine[x]]], Heap[n]);
    Nod[k] = Nod[ordine[x]];
    n--;
    Cerne(Nod[k]);
}

void Promovare(int n)
{
    int tata = n / 2;
    if (tata && Heap[n] < Heap[tata])
    {
        swap(Heap[n], Heap[tata]);
        swap(Nod[Heap[n]], Nod[Heap[tata]]);
        Promovare(tata);
    }
}

void heap_push(int x)
{
    n++;
    Heap[n] = x;
    ordine[n] = x;
    Nod[x] = n;
    Promovare(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(x);
            }
            else
                g << Heap[1] << endl;

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


}