Pagini recente » Cod sursa (job #2136653) | Cod sursa (job #2956682) | Cod sursa (job #1213020) | Cod sursa (job #2910362) | Cod sursa (job #1537752)
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MN = 200005;
const int IF = 0x3f3f3f3f;
int N,M,K;
int Heap[MN],Pos[MN],Val[MN];
inline int L(int X)
{
return (X * 2);
}
inline int R(int X)
{
return ((X * 2) + 1);
}
inline int F(int X)
{
return X / 2;
}
void Up(int X)
{
while (F(X) && Val[Heap[X]] < Val[Heap[F(X)]])
{
swap(Heap[X],Heap[F(X)]);
swap(Pos[Heap[X]],Pos[Heap[F(X)]]);
X /= 2;
}
}
void Down(int X)
{
int Y;
while (X != Y)
{
Y = X;
if (L(Y) <= K && Val[Heap[L(Y)]] < Val[Heap[X]])
X = L(Y);
if (R(Y) <= K && Val[Heap[R(Y)]] < Val[Heap[X]])
X = R(Y);
swap(Heap[X],Heap[Y]);
swap(Pos[Heap[X]],Pos[Heap[Y]]);
}
}
void Push(int X)
{
Val[++M] = X;
Heap[++K] = M;
Pos[M] = K;
Up(K);
}
void Pop(int X)
{
Val[X] = IF;
Down(Pos[X]);
K--;
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&N);
for (int i = 1;i <= N;i++)
{
int type,X;
scanf("%d",&type);
if (type < 3)
scanf("%d",&X);
if (type == 1)
{
Push(X);
continue;
}
if (type == 2)
{
Pop(X);
continue;
}
printf("%d\n",Val[Heap[1]]);
}
return 0;
}