Cod sursa(job #1979454)

Utilizator mihai2003LLL LLL mihai2003 Data 10 mai 2017 17:21:40
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
using namespace std;
int poz[200005],v[200005],h[200005];
int nrad,nh;
void schimba(int p,int q){
    int aux=h[p];
    h[p]=h[q];
    h[q]=aux;
    poz[h[p]]=p;
    poz[h[q]]=q;
}
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)schimba(p,bun),coboara(bun);
}
void urca(int p){
    if(p>1 && v[h[p]]<v[h[p/2]])
        schimba(p,p/2),urca(p/2);
}
void adauga(int x){
    h[++nh]=x;
    poz[x]=nh;
    urca(nh);
}
void sterge(int p){
    schimba(p,nh--);
    urca(p);
    coboara(p);
}
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int main()
{
    int n,tip,p;
    in>>n;
    nrad=0;
    for(int i=1;i<=n;i++){
        in>>tip;
        if(tip==1)
            in>>v[++nrad],adauga(nrad);
        if(tip==2)
            in>>p,sterge(poz[p]);
        if(tip==3)
            out<<v[h[1]]<<'\n';
    }
    return 0;
}