Pagini recente » Cod sursa (job #1673727) | Cod sursa (job #996702) | Cod sursa (job #3236011) | Cod sursa (job #1473817) | Cod sursa (job #413147)
Cod sursa(job #413147)
#include <cstdio>
#define HEAP_MAX 200010
int heap[HEAP_MAX],pos[HEAP_MAX],vec[HEAP_MAX],n=0,l=0;
#define left_son(a) (a<<1)
#define right_son(a) ((a<<1)+1)
#define father(a) (a>>1)
inline void swap(int& a,int& b){
int c=a;
a=b;
b=c;
}
void insert(int x){
while(x/2&&vec[heap[x]]<vec[heap[father(x)]]){
swap(heap[x],heap[father(x)]);
pos[heap[x]]=x;
pos[heap[father(x)]]=father(x);
x=father(x);
}
}
void remove(int x){
int y=0;
while(x!=y){
y=x;
if(left_son(y)<=l&&vec[heap[x]]>vec[heap[left_son(y)]]){
x=left_son(y);
}
if(right_son(y)<=l&&vec[heap[x]]>vec[heap[right_son(y)]]){
x=right_son(y);
}
swap(heap[x],heap[y]);
pos[heap[x]]=x,pos[heap[y]]=y;
}
}
FILE* fin=fopen("heapuri.in","r");
FILE* fout=fopen("heapuri.out","w");
int main(){
int nop,op,x;
fscanf(fin,"%d ",&nop);
for(int i=0;i<nop;i++){
fscanf(fin,"%d",&op);
switch(op){
case 1:
fscanf(fin,"%d",&x);
l++,n++;
vec[n]=x;
heap[l]=n;
pos[n]=l;
insert(l);
break;
case 2:
fscanf(fin,"%d",&x);
vec[x]=-1;
insert(pos[x]);
pos[heap[1]]=0;
heap[1]=heap[l--];
pos[heap[1]]=1;
if(l>1){
remove(1);
}
break;
case 3:
fprintf(fout,"%d\n",vec[heap[1]]);
break;
}
}
fclose(fin);
fclose(fout);
return 0;
}