Cod sursa(job #1053331)

Utilizator radulescu.serbanRadulescu Serban radulescu.serban Data 12 decembrie 2013 17:56:43
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>

using namespace std;

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

long 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]] = 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()
{
    long i,x,c;
    in >> n;
    for(i = 1 ; i <= n ; i++)
    {
        in >> c;
        if(c < 3)
            in >> x;
        if(c == 1)
        {
            l++;
            nr++;
            a[nr] = x;
            heap[l] = nr;
            pos[nr] = l;
            push(l);
        }
        if(c == 2)
        {
            a[x] = -1;
            push(pos[x]);
            pos[heap[1]] = 0;
            heap[1] = heap[l--];
            pos[heap[1]] = 1;
            if(l > 1)
                pop(1);
        }
        if(c == 3)
            out << a[heap[1]] << "\n";
    }
    return 0;
}