Pagini recente » Cod sursa (job #1517838) | Cod sursa (job #842271) | Cod sursa (job #1677918) | Cod sursa (job #1619896) | Cod sursa (job #2741831)
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
ostringstream name;
name<<x;
string l=name.str();
res += to_string(l);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int main()
{
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
int n,m;
scanf("%d %d",&n,&m);
vector<int>v(n);
int log=0;
while((1<<log)<=n){
log++;
}
vector<vector<int> >dp(n,vector<int>(log,0) );
for(int i=0;i<n;i++){
scanf("%d",&v[i]);
dp[i][0]=v[i];
}
auto pre_compute=[&](){
for(int j=1;j<log;j++){//j is power , 2^j
for(int i=0;i+(1<<j)-1<n;i++){
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
};
auto query=[&](int l,int r){
int lg=r-l+1;
int log=0;
while((1<<log)<=lg){
log++;
}
return min(dp[l-1][log-1],dp[r-(1<<(log-1)) ][log-1]);
};
pre_compute();
while(m--){
int l,r;
scanf("%d %d",&l,&r);
printf("%d\n",query(l,r));
}
return 0;
}
/*
0 1 2
1
2
3
4
5
*/