Cod sursa(job #3163182)

Utilizator dariustgameTimar Darius dariustgame Data 30 octombrie 2023 19:50:43
Problema Heapuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int heap[200000];
int lenght = -1;
vector<int> nr;

void insertEl(int x)
{
	lenght++;
	heap[lenght] = x;
	int pos = lenght;
	int p = pos >> 1;
	while (pos != 0 && heap[p] > x)
	{
		swap(heap[p], heap[pos]);
		pos = p;
		p >>= 1;
	}
}

void delEl(int x)
{
	int n = nr[x - 1];
	int i = 0;
	while (heap[i] != n)
	{
		i++;
	}
	heap[i] = heap[lenght];
	heap[lenght] = 0;
	lenght--;
	int pos = i;
	int p = pos >> 1;
	while (pos != 0 && heap[p] > n)
	{
		swap(heap[p], heap[pos]);
		pos = p;
		p >>= 1;
	}
	int c1 = pos << 1, c2 = (pos << 1) + 1;
	int aux = min(heap[c1], heap[c2]);
	if (heap[c1] == aux)
	{
		p = c1;
	}
	else
	{
		p = c2;
	}
	if (c2 > lenght) p = c1;
	while (p <= lenght && heap[p] < heap[pos])
	{
		swap(heap[p], heap[pos]);
		pos = p;
		c1 >>= 2;
		c2 = c1 + 1;
		p = min(heap[c1], heap[c2]);
	}
}

int minEl()
{
	return heap[0];
}

int main()
{
	int n;
	fin >> n;
	int x, y;
	for (int i = 0; i < n; i++)
	{
		fin >> x;
		if (x != 3)
			{
				fin >> y;
		}
		if (x == 1) 
		{ 
			insertEl(y); 
			nr.push_back(y); 
		}
		else if (x == 2) delEl(y);
		else fout << minEl() << '\n';
	}
}