Cod sursa(job #1814937)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 24 noiembrie 2016 17:59:22
Problema Range minimum query Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 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[20][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;
	for(int i = 1; i <= n; i++)
		cin >> arr[i];
	RMQ();
	int lgr2[100005];
	lgr2[1] = 0;
	for(int i = 2; i <= n; i++)
		lgr2[i] = lgr2[i / 2] + 1;
	for(int i = 0; i < m; i++){
			int a, b;
			cin >> 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;
	}	
	return(0);
}