Cod sursa(job #2191572)

Utilizator Daria09Florea Daria Daria09 Data 3 aprilie 2018 02:02:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>
#define NMAX 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[NMAX],a[NMAX],pos[NMAX],n;
int m,k;
void Insert(int k)
{
    while(k/2&&a[heap[k]]<a[heap[k/2]])
    {
        swap(heap[k],heap[k/2]);
        pos[heap[k]]=k;
        pos[heap[k/2]]=k/2;
        k/=2;
    }
}
void Eliminate(int k)
{
    int ok=1;
    while(ok!=0)
    {
        ok=0;
        int dr=k*2+1,st=k*2;
        if(st<=m)
        {
            if(dr<=m&&a[heap[dr]]<a[heap[st]])
                ok=dr;
            else
                ok=st;
            if(a[heap[k]]>a[heap[ok]])
            {
                swap(heap[k],heap[ok]);
                pos[heap[k]]=k;
                pos[heap[ok]]=ok;
                k=ok;
            }
            else
                ok=0;
        }
    }
}
void solve()
{
    int x,cod;
    f>>n;
    for(int i=1;i<=n;i++)
    {
        f>>cod;
        if(cod==3)
            g<<a[heap[1]]<<"\n";
        else
        {
            f>>x;
            if(cod==1)
            {
                ++k; ++m;
                a[k]=x;
                heap[m]=k;
                pos[k]=m;
                Insert(m);
            }
            else
            {
                a[x]=-1;
                Insert(pos[x]);
                pos[heap[1]]=0;
                heap[1]=heap[m--];
                pos[heap[1]]=1;
                if(m>1)Eliminate(1);
            }
        }
    }
}
int main()
{
    solve();
    return 0;
}