Cod sursa(job #3124257)

Utilizator Bogdy555Bogdan Bogdy555 Data 27 aprilie 2023 15:20:12
Problema Range minimum query Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>
#include <vector>



inline const int32_t Min(const int32_t _A, const int32_t _B)
{
	return _A * (_A <= _B) + _B * (_B < _A);
}



int main()
{
	std::ifstream _fIn;

	_fIn.open("rmq.in");

	if (!_fIn.is_open())
	{
		return -1;
	}

	size_t _Count = 0, _CountQueries = 0;

	_fIn >> _Count >> _CountQueries;

	std::vector<int32_t> _Vec;

	for (size_t _Index = 0; _Index < _Count; _Index++)
	{
		int32_t _Val = 0;

		_fIn >> _Val;

		_Vec.push_back(_Val);
	}

	int32_t* _PreComp = new int32_t[_Count * _Count];

	if (!_PreComp)
	{
		_fIn.close();
		return -1;
	}

	for (size_t _Index = 0; _Index < _Count * _Count; _Index++)
	{
		_PreComp[_Index] = 0;
	}

	for (size_t _Index = 0; _Index < _Count; _Index++)
	{
		_PreComp[_Index + _Index * _Count] = _Vec[_Index];
	}

	for (size_t _Iteration = 1; _Iteration < _Count; _Iteration++)
	{
		for (size_t _Line = 0; _Line < _Count - _Iteration; _Line++)
		{
			_PreComp[(_Line + _Iteration) + _Line * _Count] = Min(_PreComp[(_Line + _Iteration - 1) + _Line * _Count], _PreComp[(_Line + _Iteration) + (_Line + 1) * _Count]);
		}
	}

	std::ofstream _fOut;

	_fOut.open("rmq.out");

	if (!_fOut.is_open())
	{
		delete[] _PreComp;
		_fIn.close();
		return -1;
	}

	for (size_t _Index = 0; _Index < _CountQueries; _Index++)
	{
		size_t _Start = 0, _End = 0;

		_fIn >> _Start >> _End;

		_Start--;
		_End--;

		if (_Start >= _Count || _End >= _Count)
		{
			_fOut.close();
			delete[] _PreComp;
			_fIn.close();
			return -1;
		}

		_fOut << _PreComp[_End + _Start * _Count] << '\n';
	}

	_fOut.close();
	delete[] _PreComp;
	_fIn.close();

	return 0;
}