Cod sursa(job #1276043)

Utilizator TibixbAndrei Tiberiu Tibixb Data 25 noiembrie 2014 21:50:46
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include<fstream>
using namespace std;
int T, op, a[200003], h[200003], n, dh, poz[200003], x, c, p;
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=dh/2;
            while(p>0){
                if(a[h[c]]<a[h[p]]){
                    swap(h[c], h[p]);
                    poz[h[c]]=c;
                    poz[h[p]]=p;
                }else
                    break;
                c=p;
                p/=2;
            }
            continue;
        }
        in>>x;
        a[x]=-1;
        c=poz[x];
        p=c/2;
        while(p>0){
            if(a[h[c]]<a[h[p]]){
                swap(h[c], h[p]);
                poz[h[c]]=c;
                poz[h[p]]=p;
            }else
                break;
            c=p;
            p/=2;

        }
        h[1]=h[dh];
        poz[h[1]]=1;
        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]]){
                swap(h[c], h[p]);
                poz[h[c]]=c;
                poz[h[p]]=p;
            }else
                break;
            p=c;
            c*=2;
        }
    }
return 0;
}