Pagini recente » Cod sursa (job #1206955) | Cod sursa (job #2154151) | Cod sursa (job #104550) | Cod sursa (job #696864) | Cod sursa (job #1479853)
#include <cstdio>
#include <bits/stdc++.h>
const char iname[] = "rmq.in";
const char oname[] = "rmq.out";
const int MAXN = 100005;
int n, m, ST[MAXN][17], A[MAXN];
//void generator();
void read(){
freopen(iname, "r", stdin);
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i){
scanf("%d", A+i);
}
}
void preprocess(){
int i, j;
for(i = 0; i < n; ++i)
ST[i][0] = i;
for(j = 1; (1<<j) <= n; ++j){
for(i = 0; i+(1<<j)-1 < n; ++i)
if(A[ST[i][j-1]]<= A[ST[i + (1 << (j-1))][j-1]])
ST[i][j] = ST[i][j-1];
else
ST[i][j] = ST[i + (1<<(j-1))][j-1];
}
}
void solve(){
freopen(oname, "w", stdout);
for(int i = 0; i < m; ++i){
int a, b;
scanf("%d %d", &a, &b);
--a, --b;
int pow = 1, coef = 0;
while(pow <= b-a+1) pow <<= 1, ++coef;
pow >>= 1;
--coef;
int ans = (A[ST[a][coef]] <= A[ST[b - pow + 1][coef]]) ? ST[a][coef] : ST[b-pow+1][coef];
printf("%d\n", A[ans]);
}
}
int main()
{
// generator();
read();
preprocess();
solve();
return 0;
}