Pagini recente » Cod sursa (job #2796974) | Cod sursa (job #2115843) | Cod sursa (job #1188315) | Cod sursa (job #1829580) | Cod sursa (job #398433)
Cod sursa(job #398433)
#include <stdio.h>
#include <algorithm>
#define M 200010
using namespace std;
FILE*f=fopen("heapuri.in","r");
FILE*g=fopen("heapuri.out","w");
int h[M],nrop,i,x,n,l,a[M],pos[M];
void sus(int l){
while(l>1 && a[h[l]]<a[h[l/2]]){
swap(h[l],h[l/2]);
pos[h[l]]=l;
pos[h[l/2]]=l/2;
l/=2;
}
}
void jos(int k){
int fiu,c;
do{
fiu=0;
c=k<<k;
if(c<=l){
fiu=c;
if(c+1<=l && a[h[c]]>a[h[c+1]]){
fiu=c+1;
}
if(a[h[fiu]]>a[h[k]]){
fiu=0;
}
}
if(fiu){
swap(pos[h[fiu]],pos[h[k]]);
swap(h[fiu],h[k]);
k=fiu;
}
}while(fiu);
}
int main(){
int tip;
fscanf(f,"%d",&nrop);
for(int i=0;i<nrop;i++){
fscanf(f,"%d ",&tip);
if(tip==1){
fscanf(f,"%d",&x);
n++,l++;
a[n]=x;
pos[n]=l;
h[l]=n;
sus(l);
}
else if (tip==2){
fscanf(f,"%d",&x);
swap(h[pos[x]],h[l]);
pos[h[pos[x]]]=pos[x];
pos[h[l]]=l;
l--;
jos(pos[h[1]]);
}
else {
fprintf(g,"%d\n",a[h[1]]);
}
}
fclose(f);
fclose(g);
return 0;
}