Cod sursa(job #2252957)

Utilizator crion1999Anitei cristi crion1999 Data 3 octombrie 2018 13:38:19
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
ifstream fi("rmq.in");
ofstream fo("rmq.out");
int dp[20][MAXN];
int n, m;

void MakeDP()
{
    for(int i = 1; i <= (int)log2(n); ++i)
        for(int j = 0; j < n - (1 << i) + 1; ++j)
            dp[i][j] = min(dp[i-1][j], dp[i-1][j+(1 << i-1)]);
}


int main()
{
    int a,b;
    fi>>n>>m;
    for(int i = 0; i < n; ++i)
        fi>>dp[0][i];

    MakeDP();
    for(int i = 1; i <= m; ++i)
    {

        fi>>a>>b;
        a--;
        b--;
        int lq = (int)log2(b-a+1);
        fo<<min(dp[lq][a], dp[lq][b - (1 << lq) + 1])<<"\n";
    }
}