Cod sursa(job #2722322)

Utilizator maraboneaMara Bonea marabonea Data 12 martie 2021 19:02:31
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <cstdio>
#include <algorithm>
using namespace std;
const int kMaxN = 200005;
int N, Value[kMaxN], NH, H[kMaxN], P[kMaxN];
inline bool Compare(const int X, const int Y)
{
 return Value[X] < Value[Y];
}
inline void Swap(const int X, const int Y)
{
 swap(P[H[X]], P[H[Y]]);
 swap(H[X], H[Y]);
}
void Percolate(const int X)
{
 int F = (X>>1);
if (F && Compare(H[X], H[F]))
{
 Swap(X, F); Percolate(F);
 }
}
void Sift(const int X)
{
 int Son = (X<<1);
 Son += (Son+1 <= NH && Compare(H[Son+1], H[Son]));
 if (Son <= NH && Compare(H[Son], H[X])) {
 Swap(X, Son); Sift(Son);
 }
}
inline void Insert(const int X)
{
 H[++NH] = X, P[X] = NH;
 Percolate(P[X]);
}
inline void Erase(const int X)
{
 Swap(X, NH);
 P[H[NH]] = 0, H[NH--] = 0;
 Sift(X);
}
int main()
{
 freopen("heapuri.in", "r", stdin);
 freopen("heapuri.out", "w", stdout);
 int M; scanf("%d", &M);
for (; M; --M)
{
 int Type; scanf("%d", &Type);
 if (Type == 1)
 {
 scanf("%d", &Value[++N]);
 Insert(N);
 }
 if (Type == 2)
 {
 int X; scanf("%d", &X);
 Erase(P[X]);
 }
 if (Type == 3)
 {
 printf("%d\n", Value[H[1]]);
 }
 }
 return 0;
}