Pagini recente » Cod sursa (job #214288) | Monitorul de evaluare | Cod sursa (job #989144) | Cod sursa (job #1082121) | Cod sursa (job #2563836)
#include <bits/stdc++.h>
#include <set>
using namespace std;
set<int> ListaVecini[256];
ifstream in("rmq.in");
ofstream out("rmq.out");
int n, m, vect[256], mat[256][256];
void GenerareMatrice(){
for(int i = 1; i <= n; i++) mat[i][0] = i;
for(int j = 1; (1 << j) <= n; j++){
for(int i = 1; i <= n; i++){
if(vect[mat[i][j - 1]] < vect[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];
}
}
}
int Interogare(int x, int y){
int d = y - x + 1;
int d1 = d;
int c = 0;
while(d1) d1 /= 2, c++;
c--;
return min(vect[mat[x][c]], vect[mat[x + d - (1 << c)][c]]);
}
int main(){
in >> n >> m;
for(int i = 1; i <= n; i++) in >> vect[i];
GenerareMatrice();
/*for(int i = 1; i <= n; i++){
for(int j = 0; j <= n; j++) cout << vect[mat[i][j]] << " ";
cout << endl;
}
cout << endl;
int x, y;
for(int i = 1; i <= n; i++) cout << i << " ";
cout << endl;
for(int i = 1; i <= n; i++) cout << vect[i] << " ";
cout << endl << endl;
while(in >> x >> y){
cout << x << " - " << y << " => " << Interogare(x, y) << endl;
}*/
int x, y;
for(int i = 1; i <= m; i++){
in >> x >> y;
out << Interogare(x, y) << endl;
}
}