Cod sursa(job #1224468)

Utilizator TarabanDragosTaraban Dragos-Petru TarabanDragos Data 31 august 2014 03:23:11
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include<cstdio>
int t,n,q,a,i,j,P[200100],x[200100],h[200100],xh,p,u;
FILE *f,*g;
void chg(int u,int p){
    int aux;
    aux=h[u];
    h[u]=h[p];
    h[p]=aux;
    P[h[p]]=p;
    P[h[u]]=u;
}
int main(){
    f=fopen("heapuri.in","r");
    g=fopen("heapuri.out","w");
    fscanf(f,"%d",&t);
    while(t--){
        fscanf(f,"%d",&q);
        if(q==3)
            fprintf(g,"%d\n",x[h[1]]);
        else if(q==2){
            fscanf(f,"%d",&a);
            x[a]=-1;
            u=P[a];
            p=u/2;
            while(p>=0){
                if(x[h[u]]<x[h[p]])
                    chg(u,p);
                else
                    break;
                u=p;
                p=p/2;
            }
            h[1]=h[xh];
            P[h[1]]=1;
            xh--;
            p=1;
            u=2;
            while(u<=xh){
                if(u+1<=xh&&x[h[u+1]]<x[h[u]])
                    u++;
                if(x[h[u]]<x[h[p]])
                    chg(u,p);
                else
                    break;
                p=u;
                u*=2;
            }
        }
        else{
            fscanf(f,"%d",&a);
            x[++n]=a;
            h[++xh]=n;
            P[n]=xh;
            u=xh;
            p=xh/2;
            while(p>=0){
                if(x[h[u]]<x[h[p]])
                    chg(u,p);
                else
                    break;
                u=p;
                p=p/2;
            }
        }

    }






    fclose(f);
    fclose(g);
    return 0;
}