Pagini recente » Cod sursa (job #268022) | Cod sursa (job #1859378) | Cod sursa (job #805485) | Cod sursa (job #1541488) | Cod sursa (job #632233)
Cod sursa(job #632233)
#include <algorithm>
#include <cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define DIM 200005
int v[DIM],h[DIM],poz[DIM];
int n,m,l;
inline void upheap (int nod)
{
int tata;
for ( ; nod>1; )
{
tata=nod>>1;
if (v[h[tata]]>v[h[nod]])
{
poz[h[tata]]=nod;
poz[h[nod]]=tata;
swap (h[tata],h[nod]);
nod=tata;
}
else
return ;
}
}
inline void downheap (int nod)
{
int fiu;
for ( ; nod<=l ;)
{
if ((nod<<1)<=l)
{
fiu=nod<<1;
if (fiu+1<=l && v[h[fiu+1]]<v[h[fiu]])
++fiu;
}
else
return ;
if (v[h[nod]]>v[h[fiu]])
{
poz[h[fiu]]=nod;
poz[h[nod]]=fiu;
swap (h[fiu],h[nod]);
nod=fiu;
}
else
return ;
}
}
int main ()
{
freopen ("heapuri.in","r",stdin);
freopen ("heapuri.out","w",stdout);
int i,tip,x;
scanf ("%d",&m);
for (i=1; i<=m; ++i)
{
scanf ("%d",&tip);
if (tip==3)
printf ("%d\n",v[h[1]]);
else
{
scanf ("%d",&x);
if (tip==1)
{
v[++n]=x;
poz[h[++l]=n]=l;
upheap (l);
}
else
{
v[x]=INF;
downheap (poz[x]);
}
}
}
}