Pagini recente » Cod sursa (job #1108396) | Cod sursa (job #879908) | Cod sursa (job #2672111) | Cod sursa (job #1816313) | Cod sursa (job #1798595)
#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 16384
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
if(pbuf==BUF_SIZE){
fread(buf, BUF_SIZE, 1, fi);
pbuf=0;
}
return buf[pbuf++];
}
inline long long nextnum(){
long long a=0;
char c=nextch();
while((c<'0' || c>'9') && c!='-')
c=nextch();
int semn=1;
if(c=='-'){
semn=-1;
c=nextch();
}
while('0'<=c && c<='9'){
a=a*10+c-'0';
c=nextch();
}
return a*semn;
}
#define MAXN 200000
int val[MAXN+1], pos[MAXN+1];
int heap[MAXN+1], last;
inline int Heap_father(int p){
return p/2;
}
inline int Heap_leftson(int p){
return 2*p;
}
inline int Heap_rightson(int p){
return 2*p+1;
}
inline void Heap_Swap(int p1, int p2){
pos[heap[p1]]=p2;
pos[heap[p2]]=p1;
int aux=heap[p1];
heap[p1]=heap[p2];
heap[p2]=aux;
}
inline void Heap_upheap(int p){
while(Heap_father(p)>0 && val[heap[p]]<val[heap[Heap_father(p)]]){
Heap_Swap(p, Heap_father(p));
p=Heap_father(p);
}
}
inline void Heap_downheap(int p){
int flag=1;
int ts=-1;
while(flag){
ts=-1;
if(Heap_leftson(p)>last)
flag=0;
else{
if(val[heap[Heap_leftson(p)]]<val[heap[p]])
ts=Heap_leftson(p);
if(Heap_rightson(p)<=last && val[heap[Heap_rightson(p)]]<val[heap[Heap_leftson(p)]] && val[heap[Heap_rightson(p)]]<val[heap[p]])
ts=Heap_rightson(p);
if(ts==-1)
flag=0;
else{
Heap_Swap(p, ts);
p=ts;
}
}
}
}
inline void Heap_insert(int poz){
heap[++last]=poz;
pos[poz]=last;
Heap_upheap(last);
}
inline void Heap_erase(int poz){
int p=pos[poz];
Heap_Swap(p, last);
last--;
if(Heap_father(p)>0 && val[heap[p]]<val[heap[Heap_father(p)]])
Heap_upheap(p);
else
Heap_downheap(p);
}
int main(){
fi=fopen("heapuri.in","r");
fo=fopen("heapuri.out","w");
int n=nextnum();
int ind=1;
for(int i=1;i<=n;i++){
int c=nextnum();
if(c==1){
val[ind]=nextnum();
Heap_insert(ind);
ind++;
}
else if(c==2){
int x=nextnum();
Heap_erase(x);
}
else{
fprintf(fo,"%d\n", val[heap[1]]);
}
}
fclose(fi);
fclose(fo);
return 0;
}