Cod sursa(job #1195489)

Utilizator TibixbAndrei Tiberiu Tibixb Data 7 iunie 2014 14:07:10
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include<fstream>
using namespace std;
int T, op, n, dh, a[200010], h[200010], poz[200010], x, c, p, aux;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int main(){
    in>>T;
    for(;T--;){
        in>>op;
        if(op==3){
            out<<a[h[1]]<<"\n";
        }
        if(op==1){
            in>>x;
            a[++n]=x;
            h[++dh]=n;
            poz[n]=dh;
            c=dh;
            p=c/2;
            while(p>0){
                if( a[ h[ c ] ] < a[ h[ p ] ] ){
                    aux=h[c];
                    h[c]=h[p];
                    h[p]=aux;
                    poz[h[c]]=c;
                    poz[h[p]]=p;
                }else
                    break;

                    c=p;
                    p/=2;
            }
        }
        if(op==2){
            in>>x;
            a[x]=-1;
            c=poz[x];
            p=c/2;
            while(p>0){
                if( a[ h [c] ] < a[ h[p] ] ){
                    aux=h[c];
                    h[c]=h[p];
                    h[p]=aux;
                    poz[h[c]]=c;
                    poz[h[p]]=p;
                }else
                    break;

                    c=p;
                    p/=2;
            }
            h[1]=h[dh];
            poz[h[1]]=1;
            dh--;
            p=1;
            c=2;
            while(c<=dh){

                if(c+1<=dh && a[ h [c+1] ] < a[ h [c] ] )
                    c++;

                if( a[ h [c] ] < a[ h[p] ] ){
                    aux=h[c];
                    h[c]=h[p];
                    h[p]=aux;
                    poz[h[c]]=c;
                    poz[h[p]]=p;
                }else
                    break;

                p=c;
                c=2*p;
            }
        }
    }
return 0;
}