Pagini recente » Cod sursa (job #1367757) | Cod sursa (job #1799402) | Cod sursa (job #200282) | Cod sursa (job #2938544) | Cod sursa (job #1763804)
#include <fstream>
#define nmax 200005
using namespace std;
unsigned int h[nmax],rh[nmax],l;
int d[nmax];
void inser(unsigned int x)
{
while ( (x!=1) && ( d[h[x]] < d[h[x/2]]) )
{
swap( h[x], h[x/2]);
rh[h[x]]=x;
rh[h[x/2]]=x/2;
x/=2;
}
}
void sterg(unsigned int x)
{
unsigned int y=0;
while (x!=y)
{
y=x;
if ( (y*2<=l) && (d[h[x]]>d[h[2*y]]) ) x=2*y;
if ( (y*2+1<=l) && (d[h[x]]>d[h[2*y+1]]) ) x=2*y+1;
swap(h[x],h[y]);
rh[h[x]]=x;
rh[h[y]]=y;
}
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
unsigned int m,op,x,n=0;
for (f>>m;m;m--)
{
f>>op;
if (op==3) g<<d[h[1]]<<'\n';
else
{
f>>x;
if (op==1)
{
l++,n++;
d[n]=x;
h[l]=n;
rh[n]=l;
inser(l);
}
else
{
d[x]=-1;
inser(rh[x]);
rh[h[1]]=0;
h[1]=h[l];
rh[h[1]]=1;
l--;
if (l>1) sterg(1);
}
}
}
f.close();
g.close();
return 0;
}