Cod sursa(job #2381753)

Utilizator Anakin1001George Giorgiu Gica Anakin1001 Data 17 martie 2019 12:59:40
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int i,n,v[200001],k,a,x,h[200001],p[200001],nr;
void down_heap(int poz,int n){
    if(poz*2<=n){
        int st=poz*2;
        if(st+1<=n&&v[h[st+1]]<v[h[st]])
            st=st+1;
        if(v[h[poz]]>v[h[st]]){
            swap(p[h[poz]],p[h[st]]);
            swap(h[poz],h[st]);
            down_heap(st,k);
        }
        else
            return;
    }
    else
        return;
}
void up_heap(int poz){
    if(poz/2>=1){
        int st=poz/2;
        if(v[h[poz]]<v[h[st]]){
            swap(p[h[poz]],p[h[st]]);
            swap(h[poz],h[st]);
            up_heap(st);
        }
        else
            return;
    }
    else
        return;
}
void add_heap(int x){
    v[++nr]=x;
    h[++k]=nr;
    p[nr]=k;
    up_heap(k);
}
void delete_heap(int x){
    int pos=p[x];
    swap(p[x],p[h[k]]);
    swap(h[k],h[pos]);
    k--;
    up_heap(pos);
    down_heap(pos,k);
}
void DiscTop(){
    g<<v[h[1]]<<'\n';
}
int main()
{   f>>n;
    nr=0;
    for(i=1;i<=n;i++){
        f>>a;
        if(a==3)
            DiscTop();
        else{
            if(a==1){
                f>>x;
                add_heap(x);
            }
            else{
                f>>x;
                delete_heap(x);
            }
        }
    }
    return 0;
}