Cod sursa(job #2183925)

Utilizator JiyuuNoTsubasaMaria Guran JiyuuNoTsubasa Data 23 martie 2018 16:30:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>

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

int n,cod,x,nr,l,a[200005],heap[200005],poz[200005];
void push(int x)
{
    int aux;
    while (x/2&&a[heap[x]]<a[heap[x/2]])
    {
        aux=heap[x];
        heap[x]=heap[x/2];
        heap[x/2]=aux;
        poz[heap[x]]=x;
        poz[heap[x/2]]=x/2;
        x/=2;
    }
}

void pop(int x)
{
    int aux,y=0;
    while (x!=y)
    {
        y=x;
        if (y*2<=l&&a[heap[x]]>a[heap[y*2]])
            x=y*2;
        if (y*2+1<=l&&a[heap[x]]>a[heap[y*2+1]])
            x=y*2+1;
        aux=heap[x];
        heap[x]=heap[y];
        heap[y]=aux;
        poz[heap[x]]=x;
        poz[heap[y]]=y;
    }
}
int main()
{
    in>>n;
    for (int i=1; i<=n; i++)
    {
        in>>cod;
        if (cod<3)
            in>>x;
        if (cod==1)
        {
            l++,nr++;
            a[nr]=x;
            heap[l]=nr;
            poz[nr]=l;
            push(l);
        }
        else if (cod==2)
        {
            a[x]=-1;
            push(poz[x]);
            poz[heap[1]]=0;
            heap[1]=heap[l--];
            poz[heap[1]]=1;
            if (l>1) pop(1);
        }
        else out<<a[heap[1]]<<'\n';
    }
    return 0;
}