Cod sursa(job #2204051)

Utilizator DordeDorde Matei Dorde Data 14 mai 2018 11:24:02
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std;

const int N=200000;
int q , attime;
int n,hep[N+5],unde[N+5];

void hep_urca(int poz){
    while(poz>1 && hep[poz]<hep[poz/2]){
        swap(hep[poz],hep[poz/2]);
        poz/=2;
    }
}

void hep_insert(){
    int foo;
    scanf("%d",&foo);
    hep[++n]=foo;
    unde[++ attime] = foo;
    hep_urca(n);
}

void coboara(int foo){
    int copil = 1;
    while(copil){
        copil = 0;
        if(foo * 2 <= n )
        {
            copil = foo * 2;
            if(foo * 2 + 1 <= n && hep [foo * 2 + 1] < hep [copil])
                copil = foo * 2 + 1;
            if(hep [copil] >= hep [foo])
                copil = 0;
        }
        if(copil)
        {
            swap (hep [copil] , hep [foo]);
            foo = copil;
        }
    }
}
int position (int nr)
{
    int i;
    for(i = 1 ; i <= n ; ++ i)
        if(hep [i] == nr)
            return i;

}
void sterge(int poz){
    poz = position (unde [poz]);
    swap (hep [poz] , hep [n]);
    -- n;
    if(poz > 1 && hep [poz] > hep [poz / 2])
        hep_urca (poz);
    else
        coboara (poz);
}
void hep_delete ()
{

}
int main(){
    int scad=0;
    freopen("heap.in","r",stdin);
    freopen("heap.out","w",stdout);
    scanf("%d",&q);
    while(q--){
        int tip;
        scanf("%d",&tip);
        if(tip==1)
            hep_insert();
        if(tip==2){
            int foo;
            scanf("%d",&foo);
            sterge(foo);
            scad++;
            //cout<<"A : "<<unde[foo]<<"\n";
        }
        if(tip==3)
            printf("%d\n",hep[1]);
    }
    return 0;
}