Pagini recente » Cod sursa (job #1145908) | Cod sursa (job #2753027) | Cod sursa (job #3162589) | Cod sursa (job #2562802) | Cod sursa (job #660313)
Cod sursa(job #660313)
#include <cstdio>
#define fius( x ) ( x ) * 2
#define fiud( x ) ( x ) * 2 + 1
#define tata( x ) ( x ) / 2
#define MAX 200010
int heap[MAX],poz[MAX],nr,tip,x,dim,M,v[MAX];
void schimba(int x,int y)
{
int aux=heap[x];
heap[x]=heap[y];
heap[y]=aux; }
void up_heap(int nod)
{
while(nod>1 && v[heap[nod]]<v[heap[tata(nod)]])
{ schimba(nod,tata(nod));
poz[heap[nod]]=nod;
poz[heap[tata(nod)]]=tata(nod);
nod=tata(nod);
}
}
void down_heap(int nod)
{ while((fius(nod)<=dim && v[heap[fius(nod)]]<v[heap[nod]])||(fiud(nod)<=dim && v[heap[fiud(nod)]]<v[heap[nod]]))
if(fiud(nod)<=dim)
{ if(v[heap[fius(nod)]]<v[heap[fiud(nod)]])
{ schimba(nod,fius(nod));
poz[heap[nod]]=nod;
poz[heap[fius(nod)]]=fius(nod);
nod=fius(nod);
}
else
{ schimba(nod,fiud(nod));
poz[heap[nod]]=nod;
poz[heap[fiud(nod)]]=fiud(nod);
nod=fiud(nod);
}
}
else{ schimba(nod,fius(nod));
poz[heap[nod]]=nod;
poz[heap[fius(nod)]]=fius(nod);
nod=fius(nod);
}
}
void insert(int x)
{ v[++M]=x;
heap[++dim]=M;
poz[M]=dim;
up_heap(dim);
}
void erase(int x)
{
v[x]=-MAX;
up_heap(poz[x]);
poz[heap[1]]=0;
heap[1]=heap[dim--];
poz[heap[1]]=1;
if(dim>1 )
down_heap(1);
}
int main()
{
freopen("heapuri1.in","r",stdin);
freopen("heapuri1.out","w",stdout);
scanf("%d",&nr);
for(int i=1;i<=nr;++i)
{ scanf("%d",&tip);
if(tip==1)
{
scanf("%d",&x);
insert(x);
}
if(tip==2)
{
scanf( "%d",&x);
erase(x) ;
}
if(tip==3)
printf("%d\n",v[heap[1]]);
}
return 0;
}