Pagini recente » Cod sursa (job #827235) | Cod sursa (job #1192980) | Cod sursa (job #1856394) | Cod sursa (job #2272074) | Cod sursa (job #407103)
Cod sursa(job #407103)
#include <stdio.h>
#define MAX 200001
typedef long long tipus;
tipus h[MAX],er[MAX],poz[MAX],n=0,nn;
tipus apa(tipus x){return x/2;}
tipus jfia(tipus x){return 2*x+1;}
tipus bfia(tipus x){return 2*x;}
void perc(tipus x){
tipus key=h[x],temp;
while((x>1)&&(h[apa(x)]>key)){
temp=er[x];
h[x]=h[apa(x)];
er[x]=er[apa(x)];
poz[er[x]]=x;
x=apa(x);
er[x]=temp;
poz[er[x]]=x;
}
h[x]=key;
}
void sift(tipus x){
tipus fiu,temp;
do{
fiu=0;
if(bfia(x)<=n){
fiu=bfia(x);
if((jfia(x)<=n)&&(h[jfia(x)]<h[bfia(x)])){
fiu=jfia(x);}
if(h[x]<=h[fiu]){fiu=0;}
}
if(fiu>0){
temp=h[x];
h[x]=h[fiu];
h[fiu]=temp;
temp=er[x];
er[x]=er[fiu];
er[fiu]=temp;
poz[er[x]]=x;
poz[er[fiu]]=fiu;
x=fiu;
}
}while(fiu>0);
}
void betesz(tipus x){
n++;
h[n]=x;
er[n]=n;
poz[n]=n;
perc(n);
}
void torol(tipus x){
h[x]=h[n];
n--;
if((x>1)&&(h[x]<h[apa(x)])){
perc(x);}else{
sift(x);}
}
int main(){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
tipus i,temp1,temp2;
scanf("%lld",&nn);
for(i=1;i<=nn;i++){
scanf("%lld",&temp1);
if(temp1==3){printf("%lld\n",h[1]);}else{
scanf("%lld",&temp2);
if(temp1==2){torol(poz[temp2]);}else{ betesz(temp2);}
}
}
return 0;}