Cod sursa(job #3300656)

Utilizator brianabucur11Briana Bucur brianabucur11 Data 18 iunie 2025 13:45:32
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

const int nmax=2e5+5;

int n, nr, v[nmax], h[nmax], poz[nmax];

void push (int x)
{
    if (x/2==0)
        return;
    if (v[h[x]]<v[h[x/2]])
    {
        swap(h[x],h[x/2]);
        poz[h[x]]=x;
        poz[h[x/2]]=x/2;
        push(x/2);
    }
}

void pop (int x)
{
    int y=x;
    if (2*x<=n && v[h[2*x]]<v[h[y]])
        y=2*x;
    if (2*x+1<=n && v[h[2*x+1]]<v[h[y]])
        y=2*x+1;
    if (x!=y)
    {
        swap(h[x],h[y]);
        poz[h[x]]=x;
        poz[h[y]]=y;
        pop(y);
    }
}

int main()
{
    int q;
    fin >> q;
    for (int i=1; i<=q; i++)
    {
        int cer;
        fin >> cer;
        if (cer==1)
        {
            int val;
            fin >> val;
            v[++nr]=val;
            h[++n]=nr;
            poz[nr]=n;
            push(n);
        }
        else if (cer==2)
        {
            int val;
            fin >> val;
            v[val]=-1;
            push(poz[val]);
            poz[h[1]]=0;
            h[1]=h[n--];
            poz[h[1]]=1;
            if (n>1)
                pop(1);
        }
        else
            fout << v[h[1]] << '\n';
    }
}