Cod sursa(job #2863968)

Utilizator VipioanaMirea Oana Teodora Vipioana Data 7 martie 2022 14:14:24
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N=2e5+1;
int n,h[N],v[N],poz[N],cnt,nh;

void schimb(int p, int q){
    swap(h[p],h[q]);
    poz[h[p]]=p;
    poz[h[q]]=q;
}

void urca(int p){
    while(v[h[p]]<v[h[p/2]]){
        schimb(p,p/2);
        p/=2;
    }
}

void adauga(int cnt){
    h[++nh]=cnt;
    urca(nh);
}

void coboara(int p){
    int fs=2*p,fd=2*p+1,bun=p;
    if(fs<=nh && v[h[fs]]<v[h[bun]])
        bun=fs;
    if(fd<=nh && v[h[fd]]<v[h[bun]])
        bun=fd;
    if(bun!=p){
        schimb(p,bun);
        coboara(bun);
    }
}

void sterge(int p){
    if(p==nh)
        nh--;
    else{
        schimb(p,nh--);
        urca(p);
        coboara(p);
    }
}

int main()
{
    f>>n;
    while(n--){
        int c;
        f>>c;
        if(c==1){
            f>>v[++cnt];
            poz[cnt]=cnt;
            adauga(cnt);
        }else
        if(c==2){
            int x;
            f>>x;
            sterge(poz[x]);
        }else{
            g<<v[h[1]]<<'\n';
        }
    }
    return 0;
}