Pagini recente » Cod sursa (job #519665) | Cod sursa (job #3302371) | Cod sursa (job #3320252) | Cod sursa (job #3316869) | Cod sursa (job #2809472)
#include <stdio.h>
#include <stdlib.h>
#define LMAX 200000
int heap[LMAX],hs,p[LMAX];
static inline int parent(int i) {return (i - 1) / 2;}
static inline int leftChild(int i) {return i * 2 + 1;}
static inline int rightChild(int i) {return i * 2 + 2;}
void swap(int x, int y){
int aux = heap[y];
heap [y] = heap [x];
heap [x] = aux;
aux = p[p[x]];
p[p[x]] = p[p[y]];
p[p[y]] = aux;
}
void climbHeap(int x){
if(x == 0)
return;
if( heap[parent(x)] > heap[x] ){
swap(x, parent(x));
climbHeap(parent(x));
}
}
void downHeap(int x){ ///WHY MAN WHY
if(x >= hs)
return;
int st = leftChild(x), dr = leftChild(x), aux;
if(st == 0)
return;
if(dr != 0 && dr < st){
swap(dr,st);
aux = dr;
dr = st;
st = aux;
}
if(heap[st] < heap[x]){
swap(x, st);
downHeap(st);
}
}
void deleteElement(int x){
heap[p[x]] = heap[hs-1];
p[p[hs - 1]] = p[p[x]];
hs--;
downHeap(p[x]);
}
void add(int x, int i){
heap[hs] = x;
hs++;
p[i] = hs - 1;
climbHeap(hs-1);
}
int main(){
int n,i,t,x,j;
FILE *fin, *fout;
fin = fopen("heapuri.in","r");
fscanf(fin,"%d",&n);
fout = fopen("heapuri.out","w");
j = 0;
for(i=0;i<n;i++){
fscanf(fin,"%d",&t);
if(t == 1){
fscanf(fin,"%d",&x);
add(x,j);
j++;
} else if(t == 2){
fscanf(fin,"%d",&x);
deleteElement(x-1);
} else
fprintf(fout,"%d\n",heap[0]);
// for(int ii = 0;ii<hs;ii++){
// printf("%d ",heap[ii]);
// }
// printf("\n");
// for(int ii = 0;ii<j;ii++){
// printf("%d ",p[ii]);
// }
// printf("\n");
// printf("\n");
}
fclose(fin);
fclose(fout);
return 0;
}