Cod sursa(job #1220875)

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