Cod sursa(job #1311429)

Utilizator ducu34Albastroiu Radu Gabriel ducu34 Data 8 ianuarie 2015 10:01:26
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int h[200001],intrare[200001],n,i,tip,x,nrelecur,nrele,poz[200001];
void schimb(int x, int y)
{
    swap(h[x],h[y]);
    poz[h[x]] = x;
    poz[h[y]] = y;
}
void urcare(int p)
{
    if(p > 1 && intrare[h[p]] < intrare[h[p/2]])
    {
    	swap(poz[h[p]],poz[h[p/2]]);
    	swap(h[p],h[p/2]);
    	urcare(p/2);
    }
}
void coboarare(int x)
{
    int st = 2*x, dr = 2*x + 1, r = x;
    if (st<=nrelecur && intrare[h[st]]<intrare[h[r]]) 
        r=st;
    if (dr<=nrelecur && intrare[h[dr]]<intrare[h[r]]) 
        r=dr;
    if (r!=x)
    {
        swap(poz[h[r]],poz[h[x]]);
    	swap(h[r],h[x]);
        coboarare(x);
    }
}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>tip;
        if(tip==1)
        {
            fin>>x;
            nrele++;
            nrelecur++;
            intrare[nrele]=x;
            h[nrelecur]=nrele;
            poz[h[nrelecur]]=nrelecur;
            urcare(nrelecur);
        }
        else if(tip==2)
        {
            fin>>x;
            x = poz[x];
            schimb(x,nrelecur--);
            urcare(x);
            coboarare(x);
        }
        else
            fout<<intrare[h[1]]<<"\n";
    }
    return 0;
}