Cod sursa(job #904724)

Utilizator iulishorIulian Popescu iulishor Data 4 martie 2013 19:59:46
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
# include <fstream>
# include <algorithm>
# define dim 100005
# define lg2 0.30102999
# include <cmath>
using namespace std;
int n,m;
int mat[ dim ][ 20 ],v[ dim ];
inline void initializare()
{
	for(int i=0; i<n; ++i )
		mat[ i ][ 0 ] = i;
	for(int j=1; 1<<j <=n; ++j )
		for(int i=0; i + 1<<j -1 < n; ++i )
			if( v[ mat[ i ][ j-1 ] ] < v[ mat[ i+( 1<<( j - 1 ) ) ][ j-1 ] ] )
				mat[ i ][ j ] = mat[ i ][ j-1 ];
			else
				mat[ i ][ j ] = mat[ i+( 1<<( j-1 ) ) ][ j-1 ];
}
inline void citire()
{
	ifstream fin("rmq.in");
	ofstream fout("rmq.out");
	fin >> n >> m;
	for(int i=0; i<n; ++i )
		fin >> v[ i ];
	initializare();
	for( ; m; --m )
	{
		int i,j;
		fin >> i >> j;
		--i; --j; 
		int k = log10( double(j-i+1) ) / lg2;
		if( v[ mat[ i ][ k ] ] <= v[ mat[ j-(1<<k)+1 ][ k ] ] )
			fout << v[ mat[ i ][ k ] ] <<"\n";
		else
			fout << v[ mat[ j-(1<<k)+1 ][ k ] ] <<"\n";
	}
}
int main()
{
	citire();
}