Cod sursa(job #2970267)

Utilizator andreibrosPeta Andrei Mathias andreibros Data 24 ianuarie 2023 20:00:58
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int a[200005];
int h[200005];
int pos[200005];
int l,nr;
void ins(int x)
{
    while(x/2 && a[h[x]]<a[h[x/2]])
    {
        swap(h[x],h[x/2]);
        pos[h[x]]=x;
        pos[h[x/2]]=x/2;
        x=x/2;
    }
}
void del(int x)
{
    int i=0;
    while(i!=x)
    {
        i=x;
        if(2*i<=l && a[h[x]]>a[h[2*i]])
            x=2*i;
        if(2*i+1<=l && a[h[x]]>a[h[2*i+1]])
            x=2*i+1;
        swap(h[i],h[x]);
        pos[h[i]]=i;
        pos[h[x]]=x;
    }
}
int main()
{
    int n;
    in>>n;
    int cod,x;
    for(int i=1; i<=n; i++)
    {
        in>>cod;
        if(cod==1)
        {
            in>>x;

            nr++;
            l++;
            a[nr]=x;
            h[l]=nr;
            pos[nr]=l;
            ins(l);
        }
        else if(cod==2)
        {
            in>>x;

            a[x]=-1;
            ins(pos[x]);

            pos[h[1]]=0;
            h[1]=h[l--];
            pos[h[1]]=1;
            if(l>1)
                del(1);


        }
       else
            out<<a[h[1]]<<'\n';
    }

    return 0;
}