Pagini recente » Cod sursa (job #2951725) | Cod sursa (job #2919225) | Cod sursa (job #2408824) | Cod sursa (job #80417) | Cod sursa (job #1043507)
#include <cstdio>
using namespace std;
int h[200010],a[200010],poz[200010],i,x,y,m,n,k;
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 k)
{
int t;
if(k<=1) return;
t=k/2;
if(a[h[k]]<a[h[t]])
{
swap(k,t);
heapup(t);
}
}
void heapdown(int p, int k)
{
int i,dr,st;
if(2*p<=k)
{
st=a[h[2*p]];
if(2*p+1<=k) dr=a[h[2*p+1]];
else dr=st+1;
if(st<dr) i=2*p;
else i=2*p+1;
if(a[h[p]]>a[h[i]])
{
swap(p,i);
heapdown(i,k);
}
}
}
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",&y);
a[++k]=y;
h[++m]=k;
poz[k]=m;
heapup(m);
}
else if (x==2)
{
scanf("%d",&y);
x=poz[y];
swap(poz[y],m);
m--;
heapdown(x,m);
}
else if (x==3)
{
printf("%d\n",a[h[1]]);
}
}
return 0;
}