Pagini recente » Atasamentele paginii jc2018-runda-1 | Monitorul de evaluare | Profil katakuna | Filme | Cod sursa (job #3287948)
#include <fstream>
using namespace std;
ifstream fin ( "rmq.in" );
ofstream fout( "rmq.out" );
#define cin fin
#define cout fout
#define FR( a, b) for( int a = 0; a < b; a ++ )
#define FOR( a, c, b) for( int a = c; a < b; a ++ )
const int Nmax = 1e5 + 5, INF = 1e9;
int pow2[17], rmq[17][Nmax], semnificativ[ 2 * Nmax];
void init_pow2() {
pow2[0] = 1;
FOR( i,1, 17 )
pow2[i] = 2 * pow2[i-1];
}
void init_semnificativ() {
semnificativ[1] = 0;
int nr_crt = 1, val_crt = 1, i;
while( nr_crt <= Nmax ) {
i = nr_crt;
while( i < 2 * nr_crt ) {
semnificativ[i] = val_crt;
i++;
}
nr_crt = ( nr_crt << 1 );
val_crt++;
}
}
int query( int l, int r ) {
int exponent = semnificativ[r-l+1];
return min( rmq[exponent][l], rmq[exponent][r] );
}
int main()
{
int n, m;
cin >> n >> m;
FOR( i, 1, n + 1 )
cin >> rmq[0][i];
FOR( i, 1, 17 )
FOR( j, 1, n + 1 )
if( (j + pow2[i] ) < n )
rmq[i][j] = min( rmq[i-1][j], rmq[i-1][j+pow2[i]] );
else
rmq[i][j] = rmq[i-1][j];
FR( i, m ) {
int l, r;
cin >> l >> r;
cout << query( l, r ) << '\n';
}
return 0;
}