Pagini recente » Monitorul de evaluare | Cod sursa (job #309220) | Runda 1 preONI 2007 | Cod sursa (job #1273988) | Cod sursa (job #1480673)
# include <cstdio>
#include <vector>
using namespace std;
int op,i,n,nr,a[200010], h[200010], aux, index, j, pos[200010],r;
void fixUp(int p)
{
h[++n]=p; pos[p]=n;
p=n;
int aux;
while (p>1 && a[h[p/2]]>=a[h[p]])
{
aux =h[p/2]; h[p/2]=h[p]; h[p]=aux;
aux = pos[h[p/2]]; pos[h[p/2]]=pos[h[p]]; pos[h[p]]=aux;
p=p/2;
}
}
void fixDown(int p)
{
p = pos[p];
aux=h[p]; h[p]=h[n]; h[n]=aux;
aux=pos[h[p]]; pos[h[p]]=pos[h[n]]; pos[h[n]]=aux;
n--;
while (2*p<=n)
{
j = 2*p;
if (j+1<=n && a[h[j+1]]<a[h[j]])
{
j++;
}
if (a[h[p]]<a[h[j]]) break; else
{
aux=h[p];
h[p]=h[j];
h[j]=aux;
aux=pos[h[p]];
pos[h[p]]=pos[h[j]];
pos[h[j]]=aux;
}
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d\n",&nr);
n=0;
for (i=1; i<=nr; i++)
{
scanf("%d ",&op);
if (op==1)
{
scanf("%d\n", &a[++r]);
fixUp(r);
}
if (op==2)
{
scanf("%d\n", &index);
fixDown(index);
}
if (op==3)
{
printf("%d\n",a[h[1]]);
}
}
}