Cod sursa(job #3134064)

Utilizator tudor.pistolPistol Tudor tudor.pistol Data 28 mai 2023 03:22:47
Problema Range minimum query Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;

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

int n, m, i, j, stanga, dreapta, aux;

int main()
{
    fin >> n >> m;
    int** st = new int*[n];

    for (i = 0; i < n; i++)
    {
        st[i] = new int[(int)sqrt(n)+1];
        fin >> st[i][0];
    }
    for (j = 1; (1 << j) <= n; j++)
    {
        for (i = 0; (i + (1 << j) - 1) < n; i++)
        {
            if (st[i][j - 1] < st[i + (1 << (j-1))][j - 1])
                st[i][j] = st[i][j - 1];
            else
                st[i][j] = st[i + (1 << (j-1))][j - 1];
        }
    }
    while (m)
    {
        fin >> stanga >> dreapta;
        aux = (int)log2(dreapta - stanga + 1);
        if (st[stanga-1][aux] < st[dreapta - (1 << aux)][aux])
            fout << st[stanga-1][aux] << endl;
        else
            fout << st[dreapta - (1 << aux)][aux] << endl;
        m--;
    }

    fin.close();
    fout.close();
    return 0;
}