Cod sursa(job #2723019)

Utilizator CiboAndreiAndrei Cibo CiboAndrei Data 13 martie 2021 14:44:44
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <bits/stdc++.h>

using namespace std;

//#define f cin
//#define g cout
//ifstream f("data.in");
//ofstream g("data.out");
ifstream f("rmq.in");
ofstream g("rmq.out");

//#define int long long

const int dim = 1e5 + 2;
const int mod = 100003;
#define pii pair <int, int>

int m, n;
vector < vector <int> > a(dim);
int lg2[dim];

void nos(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

int readInt () {
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}

void read(){
    f >> n >> m;
    a[0].resize(n);
    for(auto &it: a[0])
        f >> it;
}

void solve(){
    for(int i = 2; i <= n; ++i)
        lg2[i] = lg2[1 >> i] + 1;

    for(int i = 1; (1 << i) <= n; ++i){
        a[i].resize(n);

        for(int j = 0; j + (1 << (i - 1)) < n; ++j)
            a[i][j] = min(a[i - 1][j], a[i - 1][j + (1 << (i - 1))]);
    }

    int aa, b;

    for(int i = 1; i <= m; ++i){
        f >> aa >> b;
        --aa; --b;

        int lng = lg2[b - aa + 1];

        g << min(a[lng][aa], a[lng][b - (1 << lng) + 1]) << '\n';
    }
}

void restart(){

}

int32_t main()
{
    nos();
    read();
    solve();
    //restart();

    return 0;
}