Pagini recente » Cod sursa (job #2261507) | Cod sursa (job #2362864) | Cod sursa (job #1361851) | Cod sursa (job #1483076) | Cod sursa (job #2040033)
#include <fstream>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
#define MAX 200005
int n, o, x, h[MAX], v[MAX], k, c, p[MAX];
/// h[i] = indicele elementului de la nodul i
/// v[i] = valoarea elementului al i-lea inserat
/// p[i] = pozitia elementului al i-lea inserat in heap
void afisare()
{
cout << v[h[1]] << '\n';
}
void up_heap(int poz)
{
while(poz > 1 and v[h[poz]] < v[h[poz / 2]])
{
swap(h[poz], h[poz / 2]);
swap(p[h[poz]], p[h[poz / 2]]);
poz /= 2;
}
}
void down_heap(int poz)
{
int f;
do
{
f = 0;
if(2 * poz <= k and v[h[poz]] > v[h[2 * poz]])
{
f = 2 * poz;
}
if(2 * poz + 1 <= k and v[h[poz]] > v[h[2 * poz + 1]] and v[h[2 * poz + 1]] < v[h[2 * poz]])
{
f = 2 * poz + 1;
}
if(f != 0)
{
swap(h[poz], h[f]);
swap(p[h[poz]], p[h[f]]);
poz = f;
}
}
while(f != 0);
}
void inserare(int a)
{
v[++c] = a;
h[++k] = c;
p[c] = k;
up_heap(k);
}
void stergere(int poz)
{
h[poz] = h[k];
p[h[poz]] = poz;
k--;
up_heap(poz);
down_heap(poz);
}
int main()
{
cin.sync_with_stdio(false);
cin >> n;
k = 0;
c = 0;
for(int i = 1; i <= n; ++i)
{
cin >> o;
if(o == 1)
{
cin >> x;
inserare(x);
}
else
if(o == 2)
{
cin >> x;
stergere(p[x]);
}
else
{
afisare();
}
}
return 0;
}