Pagini recente » Cod sursa (job #16939) | Cod sursa (job #2961767) | Cod sursa (job #3211352) | Cod sursa (job #2884438) | Cod sursa (job #1023029)
#include <cstdio>
const int N = 200000;
int h [N + 1];
int poz [N + 1];
int v [N + 1];
int nh;
void schimba (int p1, int p2)
{
int aux = h [p1];
h [p1] = h [p2];
h [p2] = aux;
poz [h [p1]] = p1;
poz [h [p2]] = p2;
}
void urca (int p)
{
while (p != 1 && h [p] < h [p / 2])
{
schimba (p, p / 2);
p /= 2;
}
}
void coboara (int poz)
{
int fs = 2 * poz, fd = 2 * poz + 1, bun = poz;
if (fs <= nh && h [fs] < h [bun])
bun = fs;
if (fd <= nh && h [fd] < h [bun])
bun = fd;
if (bun !=poz)
{
schimba (h [poz], h [bun]);
coboara (bun);
}
}
void adauga (int x)
{
h [++ nh] = v[nh] = x;
poz [x] = nh;
urca (nh);
}
void sterge (int poz)
{
schimba (poz, nh --);
urca (poz);
coboara (poz);
}
void init ()
{
int t, x, n, i;
freopen ("heapuri.in", "r", stdin);
freopen ("heapuri.out", "w", stdout);
scanf ("%d", &n);
for (i = 1; i <= n; i ++)
{
scanf ("%d", & t);
if (t == 1)
{
scanf ("%d", & x);
adauga (x);
}
if (t == 2)
{
scanf ("%d", & x);
sterge (poz [v[x]]);
}
if (t == 3)
printf ("%d\n", h [1]);
}
}
int main ()
{
init ();
return 0;
}