Pagini recente » Cod sursa (job #1409805) | Cod sursa (job #3129673) | Cod sursa (job #2583532) | Cod sursa (job #2152638) | Cod sursa (job #603151)
Cod sursa(job #603151)
#include <iostream>
#define NMax 200005
using namespace std;
int V[NMax], Poz[NMax], H[3*NMax], NH, N;
inline void Swap (int X, int Y)
{
int Aux;
Aux=Poz[H[X]];
Poz[H[X]]=Poz[H[Y]];
Poz[H[Y]]=Aux;
Aux=H[X];
H[X]=H[Y];
H[Y]=Aux;
}
void Percolate (int X)
{
int F=X/2;
while (F!=0)
{
if (V[H[X]]<V[H[F]])
{
Swap (X, F);
X=F;
F=X/2;
}
else
{
return;
}
}
}
void Sift (int X)
{
int S=2*X;
while (S<=NH)
{
if (S+1<=NH && V[H[S]]>V[H[S+1]])
{
++S;
}
if (V[H[X]]>V[H[S]])
{
Swap (X, S);
X=S;
S=2*X;
}
else
{
return;
}
}
}
void Insert (int X)
{
H[++NH]=X;
Poz[X]=NH;
Percolate (Poz[X]);
}
void Delete (int X)
{
int Aux=H[Poz[NH]];
Swap (X, NH);
--NH;
Sift (Poz[Aux]);
}
int main()
{
freopen ("heapuri.in", "r", stdin);
freopen ("heapuri.out", "w", stdout);
scanf ("%d", &N);
for (int i=1; i<=N; ++i)
{
int Tip;
scanf ("%d", &Tip);
if (Tip==1)
{
scanf ("%d", &V[i]);
Insert (i);
}
if (Tip==2)
{
int X;
scanf ("%d", &X);
Delete (Poz[X]);
}
if (Tip==3)
{
printf ("%d\n", V[H[1]]);
}
}
return 0;
}