Cod sursa(job #3168500)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 12 noiembrie 2023 16:30:18
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("rmq.in");
ofstream out("rmq.out");

using ll = long long;

const int NMAX = 1e5 + 5;
const int INF = 0x3f3f3f3f;

int n, m, rmq[18][NMAX];

void buildRMQ()
{
    for (int i = 1; i <= n; i++)
        in >> rmq[0][i];

    for (int i = 1, pas = 1; pas <= n; pas *= 2, i++)
        for (int j = 1; j <= n - pas; j++)
            rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + pas]);
}

int RMQ(int x, int y)
{
    int log2 = __builtin_clz(y - x + 1);
    log2 = 31 - log2;
    int pow = 1 << log2;
    return min(rmq[log2][x], rmq[log2][y - pow + 1]);
}

int main()
{
    in >> n >> m;

    buildRMQ();

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;
        out << RMQ(x, y) << '\n';
    }

    return 0;
}