Pagini recente » Cod sursa (job #899128) | Cod sursa (job #1966464) | Cod sursa (job #1335449) | Cod sursa (job #1877573) | Cod sursa (job #1498971)
#include <fstream>
#include <array>
#include <vector>
#include <cmath>
using namespace std;
template <typename T, int max_niv>
class rmq{
int n;
vector<array<T, max_niv+1>> buf;
public:
template <typename F>
rmq(const int N, F f): n(N), buf(n){
for(int i = 0; i < n; ++i){
buf[i][0] = f(i); }
for(int niv = 1, step = 1; niv <= max_niv; ++niv, step *= 2){
for(int i = 0; i+step < n; ++i){
buf[i][niv] = min(buf[i][niv-1], buf[i+step][niv-1]); } } }
int query(const int st, const int dr){
const int sz = dr-st+1, niv = log2(sz), step = 1<<niv;
return min(buf[st][niv], buf[dr-step+1][niv]); } };
int main(){
ifstream f("rmq.in");
ofstream g("rmq.out");
int n, m;
f >> n >> m;
rmq<int, 17> r(n, [&f](const int i){
int x;
f >> x;
return x; });
for(int i = 0, a, b; i < m; ++i){
f >> a >> b;
g << r.query(a-1, b-1) << '\n'; }
return 0; }