Cod sursa(job #2481428)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 26 octombrie 2019 21:29:01
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>
#define NMAX 100005

using namespace std;

ifstream f ("rmq.in");
ofstream g ("rmq.out");

int n , m , c , d;
int a[NMAX] , b[NMAX][20];

void RMQ()
{
    int k = log2(n) , i , j;

    for(i = 1 ; i <= n ; i++)
        b[i][0] = i;

    for(j = 1 ; j <= k ; j++)
        for(i = 1 ; i + (1 << j) - 1 <= n ; i++)
            if(a[ b[i][j - 1] ] < a[ b[i + (1 << (j - 1))][j - 1] ])
                b[i][j] = b[i][j - 1];
            else b[i][j] = b[i + (1 << (j - 1))][j - 1];
}

void Query(int c , int d)
{
    int k = log2(d - c + 1);

    if(a[ b[c][k] ] <= a[ b[d - (1 << k) + 1][k] ])
        g << a[ b[c][k] ] << '\n';
    else g << a[ b[d - (1 << k) + 1][k] ] << '\n';
}

int main()
{
    int i;

    f >> n >> m;

    for(i = 1 ; i <= n ; i++)
        f >> a[i];

    RMQ();

    for(i = 1 ; i <= m ; i++)
    {
        f >> c >> d;
        Query(c , d);
    }

    return 0;
}