Cod sursa(job #2887328)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 9 aprilie 2022 13:12:22
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <math.h>
using namespace std;

ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n, m, rmq[25][100005];

void precalc()
{
    int comp = log2(n); 
    for (int i = 1; i <= comp; i++)
    {
        int put = 1 << i;
        for (int cj = 0; cj + put <= n; cj++)
            rmq[i][cj] = min(rmq[i - 1][cj], rmq[i - 1][cj + (1 << (i - 1))]);
    }
}

void solve(int x, int y)
{
    int ans, k;
    k = log2(y - x + 1);
    ans = min(rmq[k][x], rmq[k][y-(1<<k)+1]);
    fout << ans << "\n";
}

int main()
{
    fin >> n >> m;
    memset(rmq, 0x3f3f3f3f, sizeof rmq);
    for (int i = 0; i < n; i++)
    {
        int val;
        fin >> val;
        rmq[0][i] = val;
    }
    precalc();
    for (int i = 0; i < m; i++)
    {
        int x, y;
        fin >> x >> y;
        x--, y--;
        solve(x, y);
    }
    return 0;
}