Cod sursa(job #1952102)

Utilizator PescaruVictorPescaru Victor PescaruVictor Data 3 aprilie 2017 22:39:11
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>

using namespace std;

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


struct strheap
{
    int val, ord;
};


int n;
int poz[200100];
strheap heap[200100];


int main()
{
    int i, lg, vf, ac, who, op, cat;
    strheap aux;

    cat=lg=0;
    fin>>n;
    for(i=1; i<=n; ++i)
    {
        fin>>op;
        if(op==1)
        {
            fin>>vf;
            ac=++lg;
            aux.ord=++cat;
            aux.val=vf;

            while( (ac/2)>=1 && aux.val<heap[ac/2].val )
            {
                heap[ac]=heap[ac/2];
                poz[heap[ac].ord]=ac;
                ac=ac/2;
            }
            heap[ac]=aux;
            poz[heap[ac].ord]=ac;

        }
        else if(op==2)
        {
            fin>>who;
            ac=poz[who];
            aux=heap[lg--];

            while(aux.val>heap[2*ac].val && 2*ac<=lg)
            {
                heap[ac]=heap[2*ac];
                poz[heap[ac].ord]=ac;
                ac=2*ac;
            }
            heap[ac]=aux;
            poz[heap[ac].ord]=ac;

        }
        else fout<<heap[1].val<<'\n';
    }
    return 0;
}