Cod sursa(job #2219068)

Utilizator AlexDabuDabu Alexandru AlexDabu Data 7 iulie 2018 01:01:27
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>

using namespace std;

#define NMAX 100002

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

int ArbMin[NMAX] , n , m;
int minim;

void Update(int pos, int val, int nod, int left, int right)
{
	if (left == right)
	{
		ArbMin[nod] = val;
		return;
	}

	int mij = (left + right) / 2;

	if (pos <= mij) Update(pos, val, 2 * nod, left, mij);
	else             Update(pos, val, 2 * nod + 1, mij + 1, right);

	(ArbMin[2 * nod] < ArbMin[2 * nod + 1]) ? (ArbMin[nod] = ArbMin[2 * nod]) : (ArbMin[nod] = ArbMin[2 * nod + 1]);
}

void Read(void)
{
	int prop;
	fin >> n >> m;
	int i;
	for (i = 1; i <= n; i++)
	{
		fin >> prop;
		Update(i, prop, 1, 1, n);
	}

	int a, b;
	for (i = 1; i <= m; i++)
	{
		fin >> a >> b;
		minim = NMAX;
		GetMin(a, b, 1, 1, n);
		fout << minim << '\n';
	}
}

void GetMin(int start, int finish, int nod, int left, int right)
{
	if (start <= left && right <= finish)
	{
		if (minim > ArbMin[nod]) minim = ArbMin[nod];
		return;
	}

	int mij = (left + right) / 2;
	if (start <= mij) GetMin(start, finish, 2 * nod, left, mij);
	if (mij < finish) GetMin(start, finish, 2 * nod + 1, mij + 1, right);
}

int main(void)
{
	Read();
	return 0;
}