Cod sursa(job #2924792)

Utilizator biancalautaruBianca Lautaru biancalautaru Data 10 octombrie 2022 20:21:02
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#define DIM 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int t,op,x,n,nr,v[DIM],h[DIM],poz[DIM];

void upHeap(int k) {
    while (k>1 && v[h[k]]<v[h[k/2]]) {
        swap(h[k],h[k/2]);
        poz[h[k]]=k;
        poz[h[k/2]]=k/2;
        k/=2;
    }
}

void downHeap(int k) {
    while (2*k<=nr) {
        int p=2*k;
        if (p+1<=nr && v[h[p+1]]<v[h[p]])
            p++;
        if (v[h[p]]<v[h[k]]) {
            swap(h[p],h[k]);
            poz[h[p]]=p;
            poz[h[k]]=k;
            k=p;
        }
        else
            break;
    }
}

int main() {
    fin>>t;
    while (t--) {
        fin>>op;
        if (op==1) {
            fin>>v[++n];
            h[++nr]=n;
            upHeap(nr);
        }
        else
            if (op==2) {
                fin>>x;
                v[x]=-1;
                upHeap(poz[x]);
                h[1]=h[nr--];
                poz[h[1]]=1;
                downHeap(1);
            }
            else
                fout<<v[h[1]]<<"\n";
    }
    return 0;
}