Cod sursa(job #2152529)

Utilizator lucia.cstCostache Lucia lucia.cst Data 5 martie 2018 17:07:32
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<fstream>
#include<cmath>

using namespace std;

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

const int MAX=2*1e5+1;

int v[MAX], h[MAX], nh, n, nr, poz[MAX], y, pz, val;

void swapp(int p, int q){
    swap(h[p], h[q]);
    poz[h[p]]=p;
    poz[h[q]]=q;
}

void up(int p){
    while(p>1 && v[h[p]]<v[h[p/2]]){
        swapp(p, p/2);
        p/=2;
    }
}

void down(int p){
    int fs=2*p, fd=2*p+1, ok=p;
    if(fs<=nh && v[h[fs]]<v[h[ok]])
        ok=fs;
    if(fd<=nh && v[h[fd]]<=v[h[ok]])
        ok=fd;
    if(ok!=p){
        swapp(p, ok);
        down(ok);
    }
}

void add(int p){
    h[++nh]=p;
    poz[p]=nh;
    up(nh);
}

void sterg(int p){
    swapp(p, nh--);
    up(p);
    down(p);
}

/*void scrie()
{
    for (int i = 1; i <= nh; i++)
    {
        cout << v[h[i]] << " ";
    }
    cout << "\n";
}
*/

int main(){
    cin>>n;
    /*for(int i=1; i<=n; i++){
        cin>>v[i];
        add(v[i]);
    }
    for(int i=1; i<=n; i++){
        cout << h[1] << " ";
        sterg(1);
    }
    */
    for(int i=0; i<n; i++){
        cin>>y;
        //pz[i]=y;
        if(y==1){
            cin>>pz;
            v[++nr]=pz;
            add(nr);
        }
        if(y==2){
            cin>>val;
            sterg(poz[val]);
        }
        if(y==3)
            cout<<v[h[1]]<<"\n";
        //scrie();
    }

    return 0;
}