Cod sursa(job #2096734)

Utilizator AkrielAkriel Akriel Data 29 decembrie 2017 17:44:51
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
int questions, totalNumbers, length[(int)(log2(N)+1)];
int lg2[N];

int *numbers[(int)(log2(N))+1]  ;

inline void readVariables(){
    freopen("rmq.in", "r", stdin);
    freopen("rmq.out", "w", stdout);
    scanf("%d %d", &totalNumbers, &questions);

    for(int index = 2;index <= totalNumbers;index++){
        lg2[index] = lg2[index >> 1] + 1;
    }

    numbers[0] = new (nothrow) int[totalNumbers+5];
    for ( int index = 1; index <= totalNumbers; index++ )
        scanf("%d", &numbers[0][index]);

    length[0] = totalNumbers;
    for ( int index = 1; index <= lg2[totalNumbers]; index++ ){
        length[index] = length[index-1] - ( 1<<(index-1) );
    }

    for ( int indexY = 1; indexY <= lg2[totalNumbers]; indexY++ ){
        numbers[indexY] = new (nothrow) int[length[indexY]+5];
        for ( int indexX = 1; indexX <= length[indexY]; indexX++ ){
            numbers[indexY][indexX] = min( numbers[indexY-1][indexX], numbers[indexY-1][indexX+(1<<(indexY-1) )] );
        }
    }

    int first, second;
    for ( int indexQuestion = 1; indexQuestion <= questions; indexQuestion++ ){
        scanf("%d %d", &first, &second);
        if ( first > second )
            swap (first, second);
        int lengthPeriod = second - first + 1;
        int indexY = lg2[lengthPeriod];
        printf("%d\n", min ( numbers[indexY][first], numbers[indexY][second-(1<<(indexY))+1] ) );
    }
}

int main(){
    readVariables();
}