Cod sursa(job #2893657)

Utilizator stefanliciuLiciu Vasile-Stefan stefanliciu Data 26 aprilie 2022 15:13:04
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#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;
}