Cod sursa(job #1988880)

Utilizator nicu_serteSerte Nicu nicu_serte Data 5 iunie 2017 01:15:54
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define nmax 200005
int h[4*nmax], v[nmax], poz[nmax], l, n;
void heapUp(int p)
{
    while(p/2>0 && v[h[p]]<v[h[p/2]])
    {
        swap(h[p], h[p/2]);
        poz[h[p]]=p;
        poz[h[p/2]]=p/2;
        p/=2;
    }
}
void inserare(int x)
{
    l++;
    n++;
    v[n]=x;
    poz[n]=l;
    h[l]=n;
    heapUp(l);
}
void heapDown(int p)
{
    int fiu;
    fiu=2*p;
    while(fiu<=l)
    {
        if(fiu+1<=l && v[h[fiu+1]]<v[h[fiu]])
            fiu++;
        if(v[h[fiu]]<v[h[p]])
        {
            swap(h[p], h[fiu]);
            poz[h[p]]=p;
            poz[h[fiu]]=fiu;
        }
        else return;
        p=fiu;
        fiu=2*fiu;
    }
}
void sterge(int x)
{
    int k;
    k=poz[x];
    h[k]=h[l];
    l--;
    if(k/2 && v[h[k/2]]>v[h[k]])
        heapUp(k);
    else heapDown(k);
}
int main()
{
    int n, op, x;
    fin>>n;
    while(n)
    {
        n--;
        fin>>op;
        if(op==1)
        {
            fin>>x;
            inserare(x);
        }
        else if(op==2)
        {
            fin>>x;
            sterge(x);
        }
        else if(op==3)
            fout<<v[h[1]]<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}