Pagini recente » Cod sursa (job #3168141) | Cod sursa (job #893980) | Cod sursa (job #782981) | Cod sursa (job #431979) | Cod sursa (job #1598622)
#include <cstdio>
#include <algorithm>
using namespace std;
int h[200005],ins[200005],poz[200005],lung;
void up(int x)
{
if(x<=1) return;
if(h[x]<h[x/2])
{
swap(poz[ins[x]],poz[ins[x/2]]);
swap(ins[x],ins[x/2]);
swap(h[x],h[x/2]);
up(x/2);
}
}
void down(int x)
{
if(2*x>lung) return;
if(2*x==lung)
{
swap(poz[ins[x]],poz[ins[2*x]]);
swap(ins[x],ins[2*x]);
swap(h[x],h[2*x]);
return;
}
if(h[2*x]<h[x] or h[2*x+1]<h[x])
{
int loc;
if(h[2*x]<h[2*x+1]) loc=2*x;
else loc=2*x+1;
swap(poz[ins[x]],poz[ins[loc]]);
swap(ins[x],ins[loc]);
swap(h[x],h[loc]);
down(loc);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int n,cnt=0,p,x,place;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&p);
if(p==1)
{
scanf("%d",&x);
cnt++;
lung++;
ins[lung]=cnt;
poz[cnt]=lung;
h[lung]=x;
up(lung);
}
if(p==2)
{
scanf("%d",&x);
place=poz[x];
h[place]=h[lung];
lung--;
up(place);
down(place);
}
if(p==3) printf("%d\n",h[1]);
}
return 0;
}