Cod sursa(job #2966819)

Utilizator jeroenodbJeroen Op de Beek jeroenodb Data 18 ianuarie 2023 15:27:38
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.74 kb
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
struct segtree {
    int ptwo;
    vector<int> seg;
    segtree(){}

    segtree(int nn) {
        ptwo=1;
        while(ptwo<nn) 
            ptwo*=2;
        seg.assign(ptwo*2,0);
    }
    auto query(int l, int r) {
        assert(l>=0 and l<ptwo and r >=0 and r<ptwo);
        l+=ptwo; r+=ptwo;
        int res = oo;
        while(l and l<=r) {
            if(l%2==1) res = min(res,seg[l++]);
            if(r%2==0) res = min(res,seg[r--]);
            l/=2; r/=2;
        }
        return res;
    }
    int& operator[](int i) {return seg[i+ptwo];}
    void build() {
        for(int i=ptwo-1;i>=1;--i) seg[i] = min(seg[i*2],seg[i*2+1]);
    }
};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef LOCAL
    freopen("rmq.in","r",stdin);
    freopen("rmq.out","w",stdout)
    #endif
    int n,m; cin >> n >> m;
    segtree seg(n);
    for(int i=0;i<n;++i) {
        cin >> seg[i];
    }
    seg.build();
    while(m--) {
        int l,r; cin >> l >> r;
        cout << seg.query(l-1,r-1) << '\n';
    }

}