Pagini recente » Autentificare | Cod sursa (job #1851840) | Cod sursa (job #72646) | Cod sursa (job #185579) | Cod sursa (job #2893606)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define N 200005
int numar_elemente;
int pozitii[N], heap[N], arr[N];
void heapify_up(int poz)
{
while (poz > 1 && arr[heap[poz]] < arr[heap[poz/ 2]])
{
swap(heap[poz], heap[poz / 2]);
pozitii[heap[poz]] = poz;
pozitii[heap[poz/ 2]] = poz / 2;
poz /= 2;
}
}
void heapify_down(int i)
{
int left_child = 2 * i;
int right_child = 2 * i + 1;
int small = i;
if (left_child <= numar_elemente && arr[heap[left_child] < arr[heap[small]]])
{
small = left_child;
}
if (right_child <= numar_elemente && arr[heap[right_child] < arr[heap[small]]])
{
small = right_child;
}
if (small != i)
{
swap(heap[i], heap[small]);
pozitii[heap[i]] = i;
pozitii[heap[small]] = small;
heapify_down(small);
}
}
void insert(int x)
{
heap[++numar_elemente] = x;
pozitii[x] = numar_elemente;
heapify_up(numar_elemente);
}
void del(int i)
{
heap[i] = heap[numar_elemente--];
pozitii[heap[i]] = i;
heapify_up(i);
heapify_down(i);
}
int main()
{
int nr, nr_operatie, x, nr_elem_vect = 0;
fin >> nr;
for (int i = 0; i < nr; ++i)
{
fin >> nr_operatie;
if (nr_operatie == 1)
{
fin >> x;
arr[++nr_elem_vect] = x;
insert(nr_elem_vect);
}
else if (nr_operatie == 2)
{
fin >> x;
del(pozitii[x]);
}
else
{
fout << arr[heap[1]] << '\n';
}
}
fin.close();
fout.close();
return 0;
}