Pagini recente » Cod sursa (job #2140236) | Cod sursa (job #2687463) | Cod sursa (job #1972864) | Cod sursa (job #436909) | Cod sursa (job #523788)
Cod sursa(job #523788)
#include <iostream>
#include <stdio.h>
#define NMAX 200001
using namespace std;
int A[NMAX],H[NMAX],P[NMAX],n,c,nr,l;
void push(int x)
{
while (x>1 && A[H[x]]<A[H[x/2]])
{
int aux=H[x];
H[x]=H[x/2];
H[x/2]=aux;
P[H[x]]=x;
P[H[x/2]]=x/2;
x/=2;
}
}
void pop(int x)
{
int y=0;
while (x!=y)
{
y=x;
if (y*2<=l && A[H[x]]>A[H[y*2]])
x=y*2;
if (y*2+1<=l && A[H[x]]>A[H[y*2+1]])
x=y*2+1;
int aux=H[x];
H[x]=H[y];
H[y]=aux;
P[H[x]]=x;
P[H[y]]=y;
}
}
void citire()
{
freopen ("heapuri.in","r",stdin);
freopen ("heapuri.out","w",stdout);
scanf ("%d",&n);
int x;
for (int i=1;i<=n;i++)
{
scanf ("%d",&c);
if (c==3)
printf("%d\n",A[H[1]]);
else
{
if (c==1)
{
scanf ("%d",&x);
l++;nr++;
A[nr]=x;
H[l]=nr;
P[nr]=l;
push(l);
}
else
{
scanf ("%d",&x);
A[x]=-1;
push(P[x]);
P[H[1]]=0;
H[1]=H[l];l--;
P[H[1]]=1;
if (l>1)
pop(1);
}
}
}
}
int main()
{
citire();
return 0;
}