Pagini recente » Cod sursa (job #141898) | Cod sursa (job #118767) | Cod sursa (job #1710383) | Cod sursa (job #425944) | Cod sursa (job #404579)
Cod sursa(job #404579)
#include<stdio.h>
int n,x,y,l,q,v[200010],h[200010],poz[200010],i;
void schimb(int i, int j)
{
int aux=h[i];
h[i]=h[j];
h[j]=aux;
}
int min(int a, int b)
{
if(a<b)return a;
return b;
}
void up(int k)
{
while(k>1&&v[h[k]]<v[h[k/2]])
{
poz[h[k]]=k/2;
poz[h[k/2]]=k;
schimb(k,k/2);
}
}
int pozmin(int k)
{
if(2*k+1<=l)
if(v[h[2*k+1]]<v[h[2*k]])return 2*k+1;
return 2*k;
}
void down(int k)
{
int aux;
if(k<=l/2)
{
aux=pozmin(k);
if(v[h[k]]>v[h[aux]])
{
poz[h[k]]=aux;
poz[h[aux]]=k;
schimb(aux,k);
down(aux);
}
}
}
void sterge(int k)
{
poz[h[k]]=l;
poz[h[l]]=k;
schimb(k,l);
l--;
down(k);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(x==1)
{
scanf("%d",&y);
v[++q]=y;
h[++l]=q;
poz[q]=l;
up(l);
}
if(x==2)
{
scanf("%d",&y);
sterge(poz[y]);
}
if(x==3)
printf("%d\n",v[h[1]]);
}
return 0;
}