Cod sursa(job #563497)
#include <stdio.h>
#define DIM 200005
int N, X, H[DIM], I[DIM];
void swap (int &a, int &b)
{
int x = a;
a = b;
b = x;
}
void urca (int n)
{
int t = n >> 1;
if (t && H[t] > H[n])
{
swap (H[t], H[n]);
swap (I[t], I[n]);
urca (t);
}
}
int main ()
{
freopen ("heapuri.in", "r", stdin);
freopen ("heapuri.out", "w", stdout);
scanf ("%d", &N);
for (int i = 0, t; i < N; i++)
{
scanf ("%d", &t);
switch (t)
{
case 1:
scanf ("%d", &X);
H[++H[0]] = X;
I[++I[0]] = H[0];
urca (H[0]);
break;
case 2:
scanf ("%d", &X);
H[X] = H[H[0]];
I[X] = I[H[0]--];
urca (X);
coboara (X);
break;
case 3:
printf ("%d ", H[1]);
break;
}
}
return 0;
}