Pagini recente » Cod sursa (job #2278966) | Cod sursa (job #582786) | Cod sursa (job #1463412) | Cod sursa (job #1681939) | Cod sursa (job #2893657)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int values[200005], h[200005], pozitii[200005];
int contor = 0;
void insert(int i)
{
while (i > 1 && values[h[i]] < values[h[i / 2]])
{
swap(h[i], h[i / 2]);
pozitii[h[i]] = i;
pozitii[h[i / 2]] = i / 2;
i /= 2;
}
}
void del(int i)
{
int left_child = 2 * i;
int right_child = 2 * i + 1;
int small = i;
do {
i = small;
left_child = 2 * i;
right_child = 2 * i + 1;
if (left_child <= contor && values[h[left_child]] < values[h[small]])
{
small = left_child;
}
if (right_child <= contor && values[h[right_child]] < values[h[small]])
{
small = right_child;
}
swap(h[i], h[small]);
pozitii[h[i]] = i;
pozitii[h[small]] = small;
} while (small != i);
}
int main()
{
int n, nr_operatie, x;
fin >> n;
for (int i = 0; i < n; ++i)
{
fin >> nr_operatie;
if (nr_operatie == 1)
{
fin >> x;
values[++contor] = x;
h[contor] = contor;
pozitii[contor] = contor;
insert(contor);
}
if (nr_operatie == 2)
{
fin >> x;
int pozitie = pozitii[x];
values[x] = 1000000001;
del(pozitie);
}
if (nr_operatie == 3)
{
fout << values[h[1]] << '\n';
}
}
return 0;
}