Pagini recente » Istoria paginii utilizator/ngocphuong912k | Cod sursa (job #1772544) | Cod sursa (job #1865624) | Atasamentele paginii Clasament rundeins | Cod sursa (job #1224468)
#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;
}