Cod sursa(job #1952152)

Utilizator PescaruVictorPescaru Victor PescaruVictor Data 3 aprilie 2017 23:01:03
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 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, next;
    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;

            if(vf==40)
                continue;
        }
        else if(op==2)
        {
            fin>>who;
            ac=poz[who];
            aux=heap[lg--];
            if(heap[2*ac].val>heap[2*ac+1].val)
                    next=2*ac+1;
            else next=2*ac;
            while(aux.val>heap[next].val && next<=lg)
            {
                heap[ac]=heap[next];
                poz[heap[ac].ord]=ac;
                ac=next;
                if(heap[2*ac].val>heap[2*ac+1].val)
                    next=2*ac+1;
                else next=2*ac;
            }
            heap[ac]=aux;
            poz[heap[ac].ord]=ac;

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