Pagini recente » Cod sursa (job #1361059) | Cod sursa (job #2382822) | Cod sursa (job #2132811) | Cod sursa (job #2914326) | Cod sursa (job #2755161)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define NMax 100005
#define LMax 19
ifstream f("rmq.in");
ofstream g("rmq.out");
int main()
{
auto rmq = new int[NMax][LMax];
vector<int> v;
int lg[NMax];
int n,x,y,m;
f >> n >> m;
for(int i =0; i< NMax; i++){
for(int j = 0; j< 18; j++){
rmq[i][j] = 1000000;
}
}
lg[1]=0;
for (int i=2;i<=n;i++)
lg[i]=lg[i/2]+1;
for(int i = 0; i< n; i++){
f >> x;
rmq[i][0] = x;
v.push_back(x);
}
int range = 2;
for(int j = 1; range <= n; j++, range*=2){
for(int i = 0; i< n; i++){
rmq[i][j] = min(rmq[i][j-1], rmq[i+range/2][j-1]);
}
}
for(int k = 0; k< m; k++){
f>> x >> y;
int t = lg[y-x+1];
g << min(rmq[x-1][t], rmq[x + t-1][t]) << "\n";
}
return 0;
}