Pagini recente » Cod sursa (job #1883733) | Cod sursa (job #1374645) | Cod sursa (job #464742) | Cod sursa (job #2015476) | Cod sursa (job #2502137)
using namespace std;
#include<iostream>
#include<fstream>
int st[100001][18], k;
int n, logg[100001], m, inc, sf;
int main() {
ifstream fin("rmq.in");
ofstream fout("rmq.out");
fin >> n >> m;
for (int i = 0; i<n; i++) {
fin >> st[i][0];
}
logg[1] = 0;
for (int i = 2; i<=n; i++) {
logg[i] = logg[i/2]+1;
}
k = logg[n];
for (int j = 1; j<=k; j++){
int x = (1<<j);
int y = x/2;
for (int i = 0; i+x<=n; i++) {
if (st[i][j-1] > st[i+y][j-1]) {
st[i][j] = st[i+y][j-1];
} else {
st[i][j] = st[i][j-1];
}
//st[i][j] = min(st[i][j-1], st[i+y][j-1]);
}
}
/*for (int i = 0; i<=n; i++) {
for (int j = 0; j<=k; j++) {
cout << st[i][j] << " ";
}
cout << endl;
}
cout << endl;*/
for (int i = 1; i<=m; i++) {
fin >> inc >> sf;
inc--;
sf--;
int j = logg[sf-inc+1];
//cout << j << endl;
fout << min(st[inc][j], st[sf-(1<<j)+1][j]) << endl;
// cout << inc << " " << j << " " << sf-(1<<j)+1 << " " << j << endl;
}
fin.close();
fout.close();
return 0;
}