Cod sursa(job #1224099)

Utilizator TibixbAndrei Tiberiu Tibixb Data 29 august 2014 17:49:55
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include<fstream>
using namespace std;
int T, op, a[200007], h[200007], c, p, poz[200007], x, dh, aux, n;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int main(){
    in>>T;
    for(;T--;){
        in>>op;
        if(op==3){
            out<<a[h[1]]<<"\n";
            continue;
        }
        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[p]]>a[h[c]]){
                    aux=h[p];
                    h[p]=h[c];
                    h[c]=aux;
                    poz[h[p]]=p;
                    poz[h[c]]=c;
                }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[p]]>a[h[c]]){
                    aux=h[p];
                    h[p]=h[c];
                    h[c]=aux;
                    poz[h[p]]=p;
                    poz[h[c]]=c;
                }
                c=p;
                p/=2;
            }
            h[1]=h[dh];
            poz[h[1]]=1;
            dh--;
            p=1;
            c=2;
            if(c+1<=dh && a[h[c+1]]<a[h[c]])
                c++;
            while(c<=dh && a[h[p]]>a[h[c]]){
                aux=h[p];
                h[p]=h[c];
                h[c]=aux;
                poz[h[p]]=p;
                poz[h[c]]=c;
            }
            p=c;
            c*=2;
        }
    }
return 0;
}