Cod sursa(job #3305243)

Utilizator robigiirimias robert robigi Data 31 iulie 2025 00:23:50
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

const int LOG = 20;
const int MAXN = 100002;
int mins[MAXN][LOG];

int main()
{
    ifstream fin("rmq.in");
    ofstream fout("rmq.out");

    int n, m;
    fin >> n >> m;

    int logn = 1;
    int count = 0;
    while (logn <= n)
    {
        logn <<= 1;
        count++;
    }

    vector<int> logs(n + 1, 0);
    logs[0] = -1;
    for (int i = 1; i <= n; ++i)
    {
        int x;
        fin >> x;
        mins[i][0] = x;
        logs[i] = logs[i / 2] + 1;
    }


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

    for (int i = 1; i <= m; ++i)
    {
        int x, y;
        fin >> x >> y;
        int logz = logs[y - x + 1];

        fout << min(mins[x][logz], mins[y + 1 - (1 << logz)][logz]) << '\n';

    }

    return 0;
}