Cod sursa(job #2290689)

Utilizator Dragomiralexandru621@yahoo.comDragomir ionut alexandru [email protected] Data 26 noiembrie 2018 20:37:00
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int nod, v[200005], heap[200005], vp[200005], l, k;
void push(int nod )
{
int var ;
while( nod / 2 != 0 && v [heap [nod]] < v [heap [nod / 2]])
{
    var = heap[nod];
    heap[nod] = heap[nod / 2];
    heap[nod / 2] = var;
    vp[heap[nod ]] = nod;
    vp[heap[nod / 2]] = nod / 2;
    nod = nod / 2;
}
}
void pop(int nod)
{
int var , t = 0;
while( nod != t)
{
    t = nod;
    if(t * 2 <= l && v[ heap[nod]] > v[ heap[ t*2]])
        nod = t * 2;
    if( t * 2 + 1 <= l && v[heap[nod]] > v[heap[t * 2 + 1]])
        nod = t * 2 + 1;
    var = heap[nod];
    heap[nod] = heap[t];
    heap[t] = var;
    vp[heap[nod ]] = nod;
    vp[heap[ t]] = t;
}
}
int main()
{
    int n, i, cod, x, y;

    f >> n;
    for( i = 1; i <= n; i++)
    {
        f >> cod;
        if( cod  == 1)
        {
            f >> x;
            l ++;
            k++;
            v[k] = x;
            heap[l] = k;
            vp[k] = l;
            push(l);
        }
        else if( cod == 2)
        {
            f >> y;
            v[y] = -1;
                push(vp[y]);
            vp[heap[1]] = 0;
            heap[1] = heap[l];
            l--;
            vp[heap[1]] = 1;
            if(l > 1)
                pop(1);

        }
            else if(cod == 3)
                g << v[heap[1]] << '\n';

    }
}