Pagini recente » Cod sursa (job #1438910) | Cod sursa (job #1438915) | Cod sursa (job #2818386) | Cod sursa (job #1205655) | Cod sursa (job #904852)
Cod sursa(job #904852)
# include <fstream>
# include <algorithm>
# define dim 100005
# define lg2 0.30102999
# include <cmath>
using namespace std;
int n,m;
long mat[ dim ][ 30 ],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( float(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();
}