Pagini recente » Cod sursa (job #3169678) | Cod sursa (job #414282) | Cod sursa (job #185922) | Cod sursa (job #2897293) | Cod sursa (job #1512600)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n, N, L;
int h[200005], a[200005], p[200005];
void Insrt(int k)
{
while (k > 1 && a[h[k]] < a[h[k/2]])
{
swap(h[k], h[k/2]);
p[h[k]] = k;
p[h[k/2]] = k / 2;
k = k / 2;
}
}
void Elim(int x)
{
int y = 0;
while (x != y)
{
y = x;
if (y * 2 <= L && a[h[x]] > a[h[y*2]])
x = y * 2;
if (y * 2 + 1 <= L && a[h[x]] > a[h[y*2+1]])
x = y * 2 + 1;
swap(h[x], h[y]);
p[h[x]] = x;
p[h[y]] = y;
}
}
int main()
{
int x, y;
fin >> n;
N = 0;
while (n--)
{
fin >> x;
if (x == 1)
{
fin >> y;
L++;
N++;
a[N] = y;
h[L] = N;
p[N] = L;
Insrt(L);
}
else if (x == 2)
{
fin >> y;
a[y] = -1;
Insrt(p[y]);
p[h[1]] = 0;
h[1] = h[L--];
p[h[1]] = 1;
Elim(1);
}
else
fout << a[h[1]] << "\n";
}
return 0;
}