Cod sursa(job #2211531)

Utilizator IustinSSurubaru Iustin IustinS Data 10 iunie 2018 19:54:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>
#define nmax 200004
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int H[nmax],n,N,x,l;
int poz[nmax],a[nmax];
int cod;

void insertHeap(int x);
void stergereHeap(int x);

int main()
{
    fin>>N;
    while (N)
        {
            fin>>cod;
            if (cod==3)
                fout<<a[H[1]]<<'\n';
            else if (cod==1)
                {
                    fin>>x;
                    n++;
                    l++;
                    a[n]=x;
                    H[l]=n;
                    poz[n]=l;
                    insertHeap(l);
                }
            else
                {
                    fin>>x;
                    a[x]=-1;
                    insertHeap(poz[x]);
                    poz[H[1]]=0;
                    H[1]=H[l--];
                    poz[H[1]]=1;
                    if (l>1)
                        stergereHeap(1);
                }
            N--;
        }
    return 0;
}
void insertHeap(int x)
{
    int aux;
    while (x/2 && a[H[x]]<a[H[x/2]])
        {
            aux=H[x];
            H[x]=H[x/2];
            H[x/2]=aux;

            poz[H[x]]=x;
            poz[H[x/2]]=x/2;
            x/=2;
        }
}
void stergereHeap(int x)
{
    int aux,y=0;
    while (x!=y)
        {
            y=x;
            if (y*2<=l && a[H[x]]>a[H[y*2]])
                x=y*2;
            if (y*2+1<=l && a[H[x]]>a[H[y*2+1]])
                x=y*2+1;

            aux=H[x];
            H[x]=H[y];
            H[y]=aux;

            poz[H[x]]=x;
            poz[H[y]]=y;
        }
}