Cod sursa(job #1934858)

Utilizator CammieCamelia Lazar Cammie Data 21 martie 2017 21:17:44
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.2 kb
#include <fstream>

#define MAXN 200002

using namespace std;

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

int heap[MAXN], lg, aux, fr[MAXN], fr_heap[MAXN];

inline void urca(int father, int son)
{
    if (father >= 1)
    {
        if (heap[father] > heap[son])
        {
            aux = heap[father];
            heap[father] = heap[son];
            heap[son] = aux;

            aux = fr_heap[father];
            fr_heap[father] = fr_heap[son];
            fr_heap[son] = aux;
            fr[fr_heap[father]] = father;
            fr[fr_heap[son]] = son;

            urca(father / 2, father);
        }
    }
}

inline void coboara(int father, int left_son, int right_son)
{
    if (left_son <= lg)
    {
        if (right_son <= lg)
        {
            if (heap[left_son] < heap[right_son])
            {
                aux = heap[left_son];
                heap[left_son] = heap[father];
                heap[father] = aux;

                aux = fr_heap[left_son];
                fr_heap[left_son] = fr[father];
                fr_heap[father] = aux;
                fr[fr_heap[father]] = father;
                fr[fr_heap[left_son]] = left_son;

                coboara(left_son, left_son * 2, left_son * 2 + 1);
            }
            else
            {
                aux = heap[right_son];
                heap[right_son] = heap[father];
                heap[father] = aux;

                aux = fr_heap[father];
                fr_heap[father] = fr[right_son];
                fr_heap[right_son] = aux;
                fr[fr_heap[father]] = father;
                fr[fr_heap[right_son]] = right_son;

                coboara(right_son, right_son * 2, right_son * 2 + 1);
            }
        }
        else
        {
            if (heap[left_son] < heap[father])
            {
                aux = heap[father];
                heap[father] = heap[left_son];
                heap[left_son] = aux;

                aux = fr[left_son];
                fr_heap[left_son] = fr[father];
                fr_heap[father] = aux;
                fr[fr_heap[father]] = father;
                fr[fr_heap[left_son]] = left_son;

                coboara(left_son, left_son * 2, left_son * 2 + 1);
            }
        }
    }
}

inline void Read()
{
    int n, cer, x;

    fin >> n;

    lg = 0;
    int contor = 0;

    for (int i = 1; i <= n; i++)
    {
        fin >> cer;

        if (cer == 1)
        {
            contor++;
            fin >> x;
            heap[++lg] = x;

            fr_heap[lg] = contor; ///pe poz i in sir se afla al fr_heap[i] - lea element introdus
            fr[contor] = lg; ///al i-lea element adaugat se afla pe por fr[i] in heap

            urca(lg / 2, lg);
        }
        else if (cer == 2)
        {
            fin >> x;
            x = fr[x];

            heap[x] = heap[lg]; lg--;
            fr_heap[x] = fr_heap[lg + 1];
            fr[fr_heap[x]] = x;

            coboara(x, x * 2, x * 2 + 1);
        }
        else
        {
            fout << heap[1] << "\n";
        }
    }
}

int main ()
{
    Read();

    fin.close(); fout.close(); return 0;
}