Cod sursa(job #2024702)

Utilizator trifangrobertRobert Trifan trifangrobert Data 21 septembrie 2017 00:24:12
Problema Range minimum query Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <algorithm>
#define DIM 100010

using namespace std;

int n, m;
int v[DIM], aint[DIM];
int answer;

inline int LeftSon(const int &x)
{
	return 2 * x;
}

inline int RightSon(const int &x)
{
	return 2 * x + 1;
}

void Build(int nod, int left, int right)
{
	if (left == right)
	{
		aint[nod] = v[left];
		return;
	}
	int mij = (left + right) / 2;
	Build(LeftSon(nod), left, mij);
	Build(RightSon(nod), mij + 1, right);
	aint[nod] = min(aint[LeftSon(nod)], aint[RightSon(nod)]);
}

void Query(int nod, int left, int right, const int &LeftQuery, const int &RightQuery)
{
	if (LeftQuery <= left && right <= RightQuery)
	{
		answer = min(answer, aint[nod]);
		return;
	}
	int mij = (left + right) / 2;
	if (LeftQuery <= mij)
		Query(LeftSon(nod), left, mij, LeftQuery, RightQuery);
	if (RightQuery >= mij + 1)
		Query(RightSon(nod), mij + 1, right, LeftQuery, RightQuery);
}

int main()
{
	ifstream f("rmq.in");
	ofstream g("rmq.out");
	f >> n >> m;
	for (int i = 1;i <= n;++i)
		f >> v[i];
	Build(1, 1, n);
	for (int i = 1;i <= m;++i)
	{
		int x, y;
		f >> x >> y;
		answer = 100010;
		Query(1, 1, n, x, y);
		g << answer << "\n";
	}
	f.close();
	g.close();
	return 0;
}