Pagini recente » Cod sursa (job #2073003) | Cod sursa (job #1291771) | Cod sursa (job #143348) | Cod sursa (job #2832942) | Cod sursa (job #2904038)
#include <fstream>
#include <iostream>
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
std::cout << matrice[0][i]<<'\n';
}
///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];
std::cout << matrice[i][j] << " ";
}
}
///construiesc rasp
int a,b;
for(int i = 0; i < m; ++i) {
fileIn >> a >> b;
std::cout << a << b << '\n';
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;
}