Cod sursa(job #1814964)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 24 noiembrie 2016 18:16:25
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;
//typedef long long ll;
//typedef long long int lli; 
//typedef pair < int, int> dbl; 
//const int maxInt = 1e9*2;
//const lli maxLong = 1e18*2;
int n, m, arr[100005]; 
int info[17][100005];
void RMQ(){
		for(int i = 1; i <= n; i++)
			info[0][i] = arr[i];
		for(int i = 1; (1 << i) <= n; i++)
				for(int j = 1; j + (1 << i) - 1 <= n; j++){
					int pw = 1 << (i-1);
					info[i][j] = min(info[i - 1][j], info[i - 1][j + pw]);
				}
}
int main(){
	//ios::sync_with_stdio(0);
	//ifstream cin("input.in");
	//ifstream cin("rmq.in");
	//ofstream cout("rmq.out");
	//cin >> n >> m;
	freopen("rmq.in", "r", stdin);
	freopen("rmq.out", "w", stdout);
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++)
		scanf("%d", &arr[i]);
		//cin >> arr[i];
	RMQ();
	//int lgr2[100005];
	int a,b;
	//lgr2[1] = 0;
	//for(int i = 2; i <= n; i++)
	//	lgr2[i] = lgr2[i / 2] + 1;
	for(int i = 0; i < m; i++){
			//cin >> a >> b;
			scanf("%d %d", &a, &b);
			int lng = b - a + 1;
			int k = int(log2(lng));
	  //  	int k = lgr2[lng];
	    	int pw = lng - (1 << k);
	    	//cout << min(info[k][a], info[k][a + pw]) << endl;
			printf("%d\n",min(info[k][a], info[k][a + pw]));
	}	
	return(0);
}