Cod sursa(job #1318742)

Utilizator dica69Alexandru Lincan dica69 Data 16 ianuarie 2015 12:17:54
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>

using namespace std;
FILE *f1 = fopen("heapuri.in","r");
FILE *f2 = fopen("heapuri.out","w");
int h[200001],i,n,o,x,p[200001],l,m,t[200001];


void push(int x)
{int aux;
while (x/2 && p[h[x]]<p[h[x/2]])
{aux=h[x];
h[x]=h[x/2];
h[x/2]=aux;
t[h[x]]=x;
t[h[x/2]]=x/2;
x=x/2;
}
}

void pop(int x)
{int aux,y=0;
while (x!=y)
{y=x;
if (y*2<=m && p[h[x]]>p[h[y*2]]) x=2*y;
if (y*2+1<=m && p[h[x]]>p[h[y*2+1]]) x=2*y+1;
aux=h[y];
h[y]=h[x];
h[x]=aux;
t[h[x]]=x;
t[h[y]]=y;
}
}


void ins(int x)
{int i;
l++;m++;h[m]=l;p[l]=x;t[l]=m;
push(l);
}

void afis()
{fprintf(f2,"%d\n",p[h[1]]);
}


void del(int x)
{int i;
p[x]=-1;
push(t[x]);
t[h[1]]=0;
h[1]=h[m--];
t[h[1]]=1;
if (l>1) pop(1);

}


int main()
{fscanf(f1,"%d",&n);
for (i=1;i<=n;i++)
{fscanf(f1,"%d",&o);
if (o==1) {fscanf(f1,"%d",&x);ins(x);}
else if (o==2) {fscanf(f1,"%d",&x);del(x);}
else afis();
}
fclose(f1);
fclose(f2);

    return 0;
}

//Challenges are what make life interesting and overcoming them is what makes life meaningful.