Pagini recente » Cod sursa (job #817644) | Cod sursa (job #2978698) | Cod sursa (job #430738) | Cod sursa (job #2059421) | Cod sursa (job #1488915)
#include<cstdio>
int t,q,nr,a,i,j,dh,h[200100],x[200100],v[200100];
FILE *f,*g;
void chg(int &a,int &b){
int aux=h[a];
h[a]=h[b];
h[b]=aux;
aux=v[ h[a] ];
v[ h[a] ]=v[ h[b] ];
v[ h[b] ]=aux;
}
void del(int a){
x[a]=-1;
int c=v[a];
int p=c/2;
while(p>=0){
if( x[ h[c] ] < x[ h[p] ] ){
chg(c,p);
c=p;
p/=2;
}
else
break;
}
h[1]=h[dh];
v[ h[1] ]=1;
dh--;
p=1;
c=2;
while(c<=dh){
if( x[ h[c] ] > x[ h[c+1] ] && c<dh )
c++;
if( x[ h[c] ] < x[ h[p] ] ){
chg(c,p);
p=c;
c*=2;
}
else
break;
}
}
void upd(int a){
x[++nr]=a;
h[++dh]=nr;
v[nr]=dh;
int c=dh;
int p=c/2;
while(p>=1){
if( x[ h[c] ] < x[ h[p] ] ){
chg(c,p);
c=p;
p/=2;
}
else
break;
}
}
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);
del(a);
}
else{
fscanf(f,"%d",&a);
upd(a);
}
}
fclose(f);
fclose(g);
return 0;
}