Pagini recente » Cod sursa (job #1195290) | Cod sursa (job #1563535) | Cod sursa (job #635897) | Cod sursa (job #1568798) | Cod sursa (job #1260729)
#include<stdio.h>
#define inf 1000000004
int v[200004],poz[200004],q,n,i,nod,nc,aux,h[200004];
int tata(int x)
{
return x/2;
}
int fiu_drept(int x)
{
return x*2;
}
int fiu_stang(int x)
{
return x*2+1;
}
int urc(int x)
{
while(v[h[x]] < v[h[tata(x)]])
{
aux = h[x];
h[x] = h[tata(x)];
h[tata(x)] = aux;
poz[h[tata(x)]] = tata(x);
poz[h[x]] = x;
x = tata(x);
}
}
int cobor(int x)
{
while((v[h[x]]>v[h[fiu_drept(x)]]&&v[h[fiu_drept(x)]]!=-1)||(v[h[x]]>v[h[fiu_stang(x)]]&&v[h[fiu_drept(x)]]!=-1))
{
if(v[h[x]]>v[h[fiu_drept(x)]])
{
aux=h[x];
h[x]=h[fiu_drept(x)];
h[fiu_drept(x)]=aux;
poz[h[fiu_drept(x)]] = fiu_drept(x);
poz[h[x]] = x;
x=fiu_drept(x);
}
else
{
aux=h[x];
h[x]=h[fiu_stang(x)];
h[fiu_stang(x)]=aux;
poz[h[fiu_stang(x)]] = fiu_stang(x);
poz[h[x]] = x;
x=fiu_stang(x);
}
}
nc=x;
}
int x;
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(i=0;i<=n;i++)
v[i]=inf;
v[0]=-1;
for(i = 1;i <= n;i ++)
{
scanf("%d",&q);
if(q == 1)
{
nod ++;
scanf("%d", &v[nod]);
h[nod] = nod;
poz[nod] = nod;
urc(nod);
}
if(q == 3)
printf("%d\n", v[h[1]]);
if(q == 2)
{
scanf("%d", &x);
int p = poz[x];
h[p] = h[nod];
nc=p;
urc(p);
cobor(nc);
}
}
return 0;
}