Cod sursa(job #2062616)

Utilizator grecubogdanGrecu Bogdan grecubogdan Data 10 noiembrie 2017 17:28:59
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N, L, NR, A[200010], Heap[200010], Pos[200010];
void push(int x)
{
    int aux;

    while (x/2 && A[Heap[x]]<A[Heap[x/2]])
    {
        aux = Heap[x];
        Heap[x] = Heap[x/2];
        Heap[x/2] = aux;

        Pos[Heap[x]] = x;
        Pos[Heap[x/2]] = x/2;
        x /= 2;
    }
}

void pop(int x)
{
    int aux, y = 0;

    while (x != y)
    {
        y = x;

        if (y*2<=L && A[Heap[x]]>A[Heap[y*2]]) x = y*2;
        if (y*2+1<=L && A[Heap[x]]>A[Heap[y*2+1]]) x = y*2+1;

        aux = Heap[x];
        Heap[x] = Heap[y];
        Heap[y] = aux;

        Pos[Heap[x]] = x;
        Pos[Heap[y]] = y;
    }
}

int main()
{
    int i, x, cod;
    f>>N;
    for (i=1; i<=N; i++)
    {
        f>>cod;
        if (cod < 3)
        {
            f>>x;
        }
        if (cod == 1)
        {
            L++, NR++;
            A[NR] = x;
            Heap[L] = NR;
            Pos[NR] = L;
            push(L);
        }
        if (cod == 2)
        {
            A[x] = -1;
            ///assert(Pos[x] != 0);
           /// assert(1<=x && x<=NR);
            push(Pos[x]);
            Pos[Heap[1]] = 0;
            Heap[1] = Heap[L--];
            Pos[Heap[1]] = 1;
            if (L>1) pop(1);
        }
        if (cod == 3) g<<"A[Heap[1]]"<<'\n';
    }

    return 0;
}