Pagini recente » Cod sursa (job #2078628) | Cod sursa (job #2954512) | Cod sursa (job #1597841) | Cod sursa (job #2933846) | Cod sursa (job #2904049)
#include <fstream>
int matrice[17][100001];
int main() {
std::fstream fileIn("rmq.in");
std::ofstream fileOut("rmq.out");
int n, m;
fileIn >> n >> m;
for(int i = 1; i <= n ; ++i) {
fileIn >> matrice[0][i]; // pe prima linie pun vectorul nostru
}
//preprocesez matricea cu minime pe intervale de puteri a lui 2
for(int i = 1 ; (1 << i) <=n; ++i) {
for(int j = 1; j <= n- (1 << i) + 1; ++j){
matrice[i][j] = (matrice[i-1][j] < matrice[i-1][(1<<(i-1)) + j]) ? matrice[i-1][j] : matrice[i-1][(1<<(i-1))+j];
}
}
//construiesc rasp
int a,b;
for(int i = 0; i < m; ++i) {
fileIn >> a >> b;
int lungime= b - a + 1;
int k = 0;
while((1<<(k + 1)) <= lungime) k++;//calculez log afland poz MSB
fileOut << ((matrice[k][a] < matrice[k][b-(1<<k) + 1]) ? matrice[k][a] : matrice[k][b - (1 << k) + 1] ) << '\n';
}
return 0;
}