Cod sursa(job #2902382)

Utilizator andlftLefter Andrei andlft Data 16 mai 2022 03:45:18
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("rmq.in");
ofstream fout("rmq.out");
    int logaritm[100001], matrice[19][100000];
    int n, m, par_1, par_2, lungime_secv, log_lungime_secv, elem_comune_suprapunere;
int main()
{

    fin >> n >> m;

    logaritm[1] = 0;
    for(int i = 2; i <=n; i++)
    {
        logaritm[i] = logaritm[i/2] + 1;
    }

    for(int i = 1; i <= n; i++)
    {
        fin>> matrice[0][i];
    }

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


    for(int i = 0; i < m; i++)
    {
        fin>>par_1>>par_2;
        lungime_secv = par_2 - par_1 + 1;
        log_lungime_secv = logaritm[lungime_secv];
        elem_comune_suprapunere = lungime_secv - (1 << log_lungime_secv);
        fout<<min(matrice[log_lungime_secv][par_1], matrice[log_lungime_secv][par_1+elem_comune_suprapunere])<<"\n";
    }
    return 0;
}