Pagini recente » Cod sursa (job #1881139) | Cod sursa (job #917072) | Cod sursa (job #818236) | Cod sursa (job #49462) | Cod sursa (job #2903604)
#include <fstream>
int main() {
std::fstream fileIn("rmq.in");
std::ofstream fileOut("rmq.out");
int matrice[17][100001];
int n, m;
fileIn >> n >> m;
for(int i = 0; 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 ; i < 17; ++i) {
for(int j = 1; j + (1 << i) - 1 < n; ++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){ //calculez log afland poz MSB
k++;
}
int minim =(matrice[k][a] < matrice[k][b- (1<<k) +1]) ? matrice[k][a] : matrice[k][ b- (1<<k) +1];
fileOut << minim;
}
return 0;
}