Pagini recente » Cod sursa (job #1141328) | Cod sursa (job #1571501) | Cod sursa (job #2466622) | Cod sursa (job #2768991) | Cod sursa (job #1043452)
#include <cstdio>
using namespace std;
int a[200002],q,poz[200002],h[200002],nr,i,n,x,numar,qq;
void swap (int x, int y)
{
int aux;
aux=h[x];
h[x]=h[y];
h[y]=aux;
poz[h[x]]=x;
poz[h[y]]=y;
}
void heapup(int i)
{
int x;
if(i<=1) return;
x=i/2;
if(a[h[i]]<a[h[x]])
{ swap(i,x);
heapup(x);}
}
void heapdown(int x)
{
int st=x*2,dr=x*2+1;
if (st<=nr)
{
int p=st;
if (dr<=nr && a[h[dr]]<a[h[st]]) p=dr;
if (a[h[p]]<a[h[x]])
{
swap(x,p);
heapdown(p);
}
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf("%d",&x);
if (x==1)
{
scanf("%d",&numar);
a[++q]=numar;
h[++nr]=q;
poz[q]=nr;
heapup(nr);
}
if(x==2)
{
scanf("%d",&numar);
qq=poz[numar];
swap(poz[numar],nr);
nr--;
heapdown(qq);
}
if(x==3)
{
printf("%d\n",a[h[1]]);
}
}
return 0;
}