Pagini recente » Cod sursa (job #1736905) | Cod sursa (job #2140822) | Cod sursa (job #2268687) | Cod sursa (job #989210) | Cod sursa (job #400523)
Cod sursa(job #400523)
#include <iostream>
using namespace std;
int n, h[200001],cond,x,poz[200001],N,nrop,v[200001];
int father(int k)
{
return k/2;
}
int fiu_stang(int k)
{
return k*2;
}
int fiu_drept(int k)
{
return k*2+1;
}
void interschimba(int y,int z)
{
int aux=h[y];
h[y]=h[z];
h[z]=aux;
aux=poz[h[y]];
poz[h[y]]=poz[h[z]];
poz[h[z]]=aux;
}
void urca(int k)
{
while((k>1)&&(v[h[father(k)]]>v[h[k]]))
{
interschimba(k,father(k));
k=father(k);
}
}
void insert(int x)
{
v[++N]=x;
h[++n]=N;
poz[N]=n;
urca(n);
}
void coboara(int k)
{
int fiu;
do
{
fiu=0;
if(fiu_stang(k)<=n)
{
fiu=fiu_stang(k);
if((fiu_drept(k)<=n)&&(v[h[fiu_drept(k)]]<v[h[fiu]]))
fiu=fiu_drept(k);
if(v[h[fiu]]>=v[h[k]])
fiu=0;
}
if(fiu)
{
interschimba(k,fiu);
k=fiu;
}
}
while(fiu);
}
void sterge(int pozitie)
{
int k=poz[pozitie];
h[k]=h[n];
n--;
poz[h[k]]=k;
if((k>1)&&(v[h[father(k)]]>v[h[k]]))
urca(k);
else coboara(k);
}
int main()
{
freopen("heapuri.out","w",stdout);
freopen("heapuri.in","r",stdin);
scanf("%d",&nrop);
for(int i=1;i<=nrop;i++)
{
scanf("%d",&cond);
if(cond==1)
{
scanf("%d",&x);
insert(x);
}
else if(cond==2)
{
scanf("%d",&x);
sterge(x);
}
else {
printf("%d ",v[h[1]]);
printf("\n");
}
}
return 0;
}