Pagini recente » Cod sursa (job #1538456) | Cod sursa (job #3186913) | Cod sursa (job #1635828) | Cod sursa (job #2322744) | Cod sursa (job #2924128)
#include <fstream>
using namespace std;
ifstream in ("heapuri.in");
ofstream out ("heapuri.out");
const int max_size = 2e5 + 1;
int h[max_size], a[max_size], poz[max_size], n, l;
void push (int x)
{
while (x / 2 && a[h[x]] < a[h[x / 2]])
{
swap(h[x], h[x / 2]);
poz[h[x]] = x;
poz[h[x / 2]] = x / 2;
x /= 2;
}
}
void pop (int x)
{
int y = 0;
while (x != y)
{
y = x;
if (y * 2 <= l && a[h[x]] > a[h[2 * y]])
{
x = 2 * y;
}
if (y * 2 + 1 <= l && a[h[x]] > a[h[2 * y + 1]])
{
x = 2 * y + 1;
}
swap(h[x], h[y]);
poz[h[x]] = x;
poz[h[x]] = y;
}
}
int main ()
{
int q;
in >> q;
while (q--)
{
int x, y;
in >> x;
if (x < 3)
{
in >> y;
}
if (x == 1)
{
l++;
n++;
a[n] = y;
h[l] = n;
poz[n] = l;
push(l);
}
if (x == 2)
{
a[y]= -1;
push(poz[y]);
poz[h[1]] = 0;
h[1] = h[l--];
poz[h[1]] = 1;
if (l > 1)
{
pop(1);
}
}
if (x == 3)
{
out << a[h[1]] << '\n';
}
}
in.close();
out.close();
return 0;
}