Cod sursa(job #3163195)

Utilizator dariustgameTimar Darius dariustgame Data 30 octombrie 2023 20:25:01
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <vector>

using namespace std;

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

int heap[200001];
int lenght = 0;
vector<int> nr;

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

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

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

int main()
{
	int cnt = 0;
	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';
			cnt++;
			if (cnt == 146)
			{
				fout << "";
			}
		}
	}
}