Pagini recente » Cod sursa (job #1909362) | Cod sursa (job #881274) | Cod sursa (job #547281) | Cod sursa (job #65339) | Cod sursa (job #1362011)
#include <fstream>
#include <algorithm>
using namespace std;
#define N 100000
int h[N],p[N],rp[N],n=0;
void heap_up(int k)
{
while(k/2>=1&&h[k/2]>h[k])
{
swap(h[k],h[k/2]);
swap(p[rp[k]],p[rp[k/2]]);
swap(rp[k],rp[k/2]);
k=k/2;
}
}
void heap_down(int k)
{
int f;
do{
f = 0;
// Alege un fiu mai mare ca tatal.
if(k*2<=n)
{
f=k*2;
if (k*2+1<=n&&h[k*2+1]<h[k*2])
f++;
if(h[f]>=h[k])
f=0;
}
if(f)
{
swap(h[k],h[f]);
swap(p[rp[k]],p[rp[f]]);
swap(rp[k],rp[f]);
k=f;
}
}while(f);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int m,s,x,i;
scanf("%d",&m);
while(m--)
{
scanf("%d",&s);
if(s==1)
{
scanf("%d",&x);
n++;
h[n]=x;
p[n]=n;
rp[n]=n;
heap_up(n);
}
else if(s==2)
{
scanf("%d",&x);
x=p[x];
swap(h[x],h[n]);
swap(p[rp[x]],p[rp[n]]);
swap(rp[x],rp[n]);
n--;
if(x/2>=0&&h[x]<h[x/2])
heap_up(x);
else
heap_down(x);
}
else
printf("%d\n",h[1]);
}
return 0;
}