Cod sursa(job #3121284)

Utilizator ridicheTudor Diaconu ridiche Data 11 aprilie 2023 17:43:18
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <cmath>

using namespace std;

ifstream in;
ofstream out;

int st[20][100005];

/*int log2(int x)
{
    if (x == 1)
        return 0;
    return log2(x/2)+1;
}*/

int main()
{
    in.open("rmq.in");
    out.open("rmq.out");
    int n, m;
    in >> n >> m;
    for (int i = 0; i < n; i++)
    {
        in >> st[0][i];
        //out << st[0][i] << " ";
    }
    //out << "\n";
    int ss = log2(n)+1;
    for (int i = 1; i < ss; i++)
    {
        for (int j = 0; j < n-(1<<i)+1; j++)
        {
            st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
            //out << st[i][j] << " ";
        }
        //out << "\n";
    }
    for (int i = 0; i < m; i++)
    {
        int x, y;
        in >> x >> y;
        x--; y--;
        int l = log2(y-x+1);
        out << min(st[l][x], st[l][y-(1<<l)+1]) << "\n";
    }
    return 0;
}