Cod sursa(job #922796)

Utilizator CrescentselectJicol Crescent Crescentselect Data 22 martie 2013 17:33:44
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include<fstream>

using namespace std;
ifstream in ("heapuri.in");
ofstream out("heapuri.out");

typedef int cactus;

# define N 200009

cactus n ,v[N],pozitii[N],lungime,poz,h[N];

inline void schimb (cactus &a, cactus &b)
{
    cactus tmp=a;
    a=b;
    b=tmp;
}
void urca(cactus x)
{
    while (x> 1 && v[h[x]]<v[h[x/2]])
    {
        schimb(h[x],h[x/2]);
        pozitii[h[x]]=x;
        pozitii[h[x/2]]=x/2;
        x/=2;
    }
}
void coboara (cactus x)
{
    cactus fs= 2*x;
    cactus fd=x*2+1;
    cactus bun =x;
    if(fs<=lungime && v[h[fs]]<v[h[bun]])
        bun=fs;
    if (fd<=lungime && v[h[fd]]<v[h[bun]])
        bun=fd;
    if(bun !=x)
    {
        schimb(h[bun],h[x]);
        pozitii [h[bun]]=bun;
        pozitii[h[x]]=x;
        coboara (bun);
    }
}
void sterge(cactus x)
{
    h[x]=h[lungime--];
    pozitii[h[x]]=x;
    if(x>1 && v[h[x]] < v[h[x/2]])
    {
        urca(x);
    }
    coboara(x);
}
void proces()
{
    in >> n;
    cactus tmp,x;
    for( cactus i=1;i<=n;i++)
    {
        in >>tmp;
        if (tmp==1)
        {
            in>>v[++poz];
            h[++lungime]=poz;
            pozitii[h[lungime]] = lungime;
            urca(lungime);
        }
        else if (tmp==2)
        {
            in>>x;
            sterge(pozitii[x]);
        }
        else
        {
            out<<v[h[1]]<<"\n";
        }
    }
}
int main()
{
    proces();
    return 0;
}