Cod sursa(job #2738364)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 5 aprilie 2021 19:06:52
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;

const string FILENAME = "rmq";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");

#define pb push_back
#define mp make_pair

using ll = long long;

const int nmax = 100000;

int a[nmax + 5];
int rmq[20][nmax + 5];
int n, m;
int logs[nmax + 5];
int main()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
        fin >> rmq[0][i];
    logs[1] = 0;
    for(int i = 2; i <= n; i++)
        logs[i] = logs[i / 2] + 1;
    for(int i = 1; (1 << i) <= n; i++)
        for(int j = (1 << i); j <= n; j++)
            rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j - (1 << (i - 1))]);
    while(m--)
    {
        fin >> x >> y;
        int k = logs[y - x + 1];
        fout << min(rmq[k][x + (1 << k) - 1], rmq[k][y]) << "\n";
    }
    fin.close();
    fout.close();
	return 0;
}