Pagini recente » Cod sursa (job #1783583) | Cod sursa (job #3261879) | Cod sursa (job #1125014) | Cod sursa (job #3269093) | Cod sursa (job #1932503)
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
ifstream fin ("rmq.in");
ofstream fout ("rmq.out");
int questions, totalNumbers, length[(int)(log2(N)+1)];
int *numbers[(int)(log2(N))+1] ;
inline void readVariables(){
fin >> totalNumbers >> questions;
numbers[0] = new (nothrow) int[totalNumbers+5];
for ( int index = 1; index <= totalNumbers; index++ )
fin >> numbers[0][index];
length[0] = totalNumbers;
for ( int index = 1; index <= (int)(log2(totalNumbers)); index++ ){
length[index] = length[index-1] - ( 1<<(index-1) );
}
for ( int indexY = 1; indexY <= (int)(log2(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++ ){
fin >> first >> second;
if ( first > second )
swap (first, second);
int lengthPeriod = second - first + 1;
int maxPeriod = 1 << ((int)(log2(lengthPeriod)));
int indexY = log2(maxPeriod);
fout << min ( numbers[indexY][first], numbers[indexY][second-(1<<(indexY))+1] ) << "\n";
}
}
int main(){
readVariables();
}