Cod sursa(job #1519210)

Utilizator Vertex10Alexandru Pokharel Vertex10 Data 6 noiembrie 2015 23:44:31
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#define N 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int h[N], v[N], poz[N], nh, nr, n, x;
void schimb(int p, int q)
{
    swap (h[q], h[p]);
    poz[h[p]]=p;
    poz[h[q]]=q;
}
void coboara(int p)
{
    int bun=p, fs=2*p, fd=2*p+1;
    if(fs<=nh && v[h[fs]]<v[h[bun]])
        bun=fs;
    if(fd<=nh && v[h[fd]]<v[h[bun]])
        bun=fd;
    if(bun!=p)
    {
        schimb(bun, p);
        coboara(bun);
    }
}
void urca(int p)
{
    while(p>1 && v[h[p]]<v[h[p/2]])
    {
        schimb(p, p/2);
        p/=2;
    }
}
int main()
{
    int tip;
    f >> n;
    for (int i=1; i<=n; ++i)
    {
        f >> tip;
        if(tip==1)
        {
            f >> v[++nr];
            h[++nh] = nr;
            poz[nr]=nh;
            urca(nh);
        }
        if(tip==2)
        {
            f >> x;
            int p = poz[x];
            schimb(p, nh);
            --nh;
            urca(p);
            coboara(p);
        }
        if (tip==3)
        {
            g << v[h[1]] << "\n";
        }
    }
    return 0;
}