Cod sursa(job #2897232)

Utilizator bubblegumixUdrea Robert bubblegumix Data 2 mai 2022 23:32:22
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.54 kb
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define lsb(x) ((x) & (-x))
#define max(...) max({__VA_ARGS__})
#define min(...) min({__VA_ARGS__})
#define hmap unordered_map
#define var auto &
#define hset unordered_set
#define pq priority_queue
#define exists(x, v) (v.find(x) != v.end())
#define inrange(x, a, b) (x >= a && x <= b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef pair<ull, ull> p64_u;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef HOME
    ~debug() { cerr << endl; }
    eni(!= ) cerr << boolalpha << i; ris;
}
eni(== ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";

}

#else
    sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define darr(x,n) debug() <<#x<<": "<< range(x,x+n+1)
#define dmat(x,n,m) for(int i=0;i<=n;i++) if(!i) debug() <<#x << ":" << range(x[i],x[i]+m+1); else debug() << range(x[i],x[i]+m+1);
const int lim = 1e5 + 5;

int sparse[lim][20];
int a[lim];
int lg[lim];
int n, m;
int main()
{
    freopen("rmq.in", "r", stdin);
    freopen("rmq.out", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    lg[1] = 0;
    for (int i = 2; i < lim; i++)
        lg[i] = 1 + lg[i / 2];

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sparse[i][0] = a[i];
    }

    for (int j = 1; j < 17; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
            sparse[i][j] = min(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]);

    while (m--)
    {
        int l, r;
        cin >> l >> r;
        int dist = r - l + 1;
        int j = lg[dist];
        cout << min(sparse[l][j], sparse[r - (1 << j) + 1][j]) << "\n";
    }

    return 0;
}