Pagini recente » Cod sursa (job #729863) | Cod sursa (job #1039797) | Cod sursa (job #1660811) | Cod sursa (job #3181708) | Cod sursa (job #1789391)
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 200000;
int heap[4 * NMAX + 5], poz[NMAX + 5];
int v[NMAX + 5], n, p, k, st, dr, best;
inline void Myswap(int a, int b)
{
swap(poz[a], poz[b]);
swap(heap[a], heap[b]);
}
inline void adaug(int x)
{
v[++n] = x;
heap[++k] = n;
poz[n] = k;
p = k;
while (p > 1 && v[heap[p]] < v[heap[p >> 1]])
{
Myswap(p, (p >> 1));
p >>= 1;
}
}
inline void elimin(int x)
{
p = poz[x];
Myswap(p, k);
--k;
while (p > 1 && v[heap[p]] < v[heap[p >> 1]])
{
Myswap(p, (p >> 1));
p >>= 1;
}
while ((p << 1) <= k)
{
st = (p << 1); dr = st + 1;
best = st;
if (dr <= k && v[heap[st]] > v[heap[dr]])
best = dr;
if (v[heap[p]] > v[heap[best]])
Myswap(p, best);
else
break;
p = best;
}
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int m, test, x;
scanf("%d", &m);
for (register int op = 1; op <= m; ++op)
{
scanf("%d", &test);
if (test == 1)
{
scanf("%d", &x);
adaug(x);
}
else if (test == 2)
{
scanf("%d", &x);
elimin(x);
}
else
printf("%d\n", v[heap[1]]);
}
return 0;
}