Cod sursa(job #1160785)

Utilizator lilian_ciobanuLilian Ciobanu lilian_ciobanu Data 30 martie 2014 19:48:24
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include<fstream>
#include<algorithm>

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

const int NMAX=2*200009;

struct QQ{
    int val;
    int poz;
}h[NMAX];

int p[NMAX],nr,in;



void urca(int pz){
    QQ t=h[pz];

    while(pz>1 && t.val < h[pz/2].val){
        h[pz]=h[pz/2];
        p[h[pz].poz]=pz;
        pz/=2;
    }

    h[pz]=t;
    p[h[pz].poz]=pz;
}


void coboara(int pz){

    int ok;
    do{
        ok=0;
        if(2*pz <=nr ){
          ok=2*pz;
          if(2*pz +1 <=nr && h[2*pz].val > h[2*pz+1].val)
            ok=2*pz+1;

          if(h[pz].val < h[ok].val)
                ok=0;

          if(ok){
            swap(p[h[pz].poz], p[h[ok].poz]);
            swap(h[pz],h[ok]);

            pz=ok;
          }
        }




    }while(ok);


}


void dell(int pz){
    h[pz]=h[nr];
    p[h[pz].poz]=pz;

    if(pz>1 && h[pz].val < h[pz/2].val)
        urca(pz);
    else
        coboara(pz);

}

void insr(int x){
    nr++;
    h[nr].val=x;
    h[nr].poz=in;
    p[in]=nr;

    urca(nr);
}

int main(){
    int n,i,j,x,y;

    f>>n;
    for(i=1; i<=n; ++i){
        f>>x;
        if(x==1){
            f>>y;
            in++;
            insr(y);
        }
        else
        if(x==2){
            f>>y;
            dell(p[y]);
        }
        else{
            g<<h[1].val<<'\n';
        }

    }
/*
for(i=1; i<=nr; ++i)
    g<<h[i].val<<' ';
*/
return 0;
}