Cod sursa(job #1930258)

Utilizator Alex18maiAlex Enache Alex18mai Data 18 martie 2017 17:39:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <fstream>

using namespace std;

ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

int v[200010];
int h[200010];
int sz,n;
bool deleted[200010];

void UpHeap (int nod){
    while ((nod >> 1) > 0){
        if (v[h[nod >> 1]] > v[h[nod]]){
            swap (h[nod >> 1] , h[nod]);
            nod=(nod >> 1);
        }
        else{
            return;
        }
    }
    return;
}

void DownHeap (int nod){
    while (nod<=sz){
        if (nod * 2 <= sz && nod* 2 + 1<=sz && v[h[nod * 2]] < v[h[nod]] && v[h[nod* 2+1]] < v[h[nod]]){
            if (v[h[nod* 2]] < v[h[nod* 2+1]]){
                swap (h[nod],h[nod* 2]);
                nod=nod* 2;
            }
            else{
                swap (h[nod], h[nod* 2 + 1]);
                nod=nod* 2 + 1;
            }
        }
        else if (nod* 2 <= sz && v[h[nod* 2]] < v[h[nod]]){
            swap (h[nod],h[nod* 2]);
            nod=nod* 2;
        }
        else if (nod* 2 + 1<=sz && v[h[nod* 2+1]] < v[h[nod]]){
            swap (h[nod], h[nod* 2 + 1]);
            nod=nod * 2 + 1;
        }
        else {
            return;
        }
    }
    return;
}

void TypeOne (int x){
    n+=1;
    v[n]=x;
    sz+=1;
    h[sz]=n;
    UpHeap (sz);
}

void TypeTwo (int x) {
    deleted[x]=1;
}

int TypeThree (){
    while (deleted[h[1]] == 1){
        swap (h[sz], h[1]);
        sz-=1;
        DownHeap(1);
    }
    return v[h[1]];
}


int main()
{
    int t;
    cin>>t;
    for (int i=1; i<=t; i++){
        int type;
        cin>>type;
        int x;
        if (type == 1){
            cin>>x;
            TypeOne(x);
        }
        else if (type == 2){
            cin>>x;
            TypeTwo(x);
        }
        else if (type == 3){
            cout<<TypeThree()<<'\n';
        }
    }
    return 0;
}