Cod sursa(job #2853846)

Utilizator XyanEusebiu Pusca Xyan Data 20 februarie 2022 17:49:22
Problema Range minimum query Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
// RMQ.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int st[100010][18];
int N, M;
void gen(int grad)
{
    for (int i = 1; i + (1 << (grad - 1)) <= N; i++)
        st[i][grad] = min(st[i][grad - 1], st[i + (1 << grad - 1)][grad - 1]);
    if (1 << ++grad <= N)
        gen(grad);
}

int main()
{
    fin >> N >> M;
    for (int i = 1; i <= N; i++)
        fin >> st[i][0];
    gen(1);
    int x, y;
    for (int i = 0; i < M; i++)
    {
        fin >> x >> y;
        int lg = log2(y - x + 1);
        fout << min(st[x][lg], st[y - (1 << lg) + 1][lg]) << endl;
    }
    return 0;
}