Cod sursa(job #1814815)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 24 noiembrie 2016 16:33:55
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 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[1<<17]; 
int info[1<<17][17];
void RMQ(){
		for(int i = 0; i < n; i++)
			info[i][0] = i;
		int i = 0; 
		int j = 1;
		while( (1 << j) < n ){
				if(arr[info[i][j - 1]] < arr[info[i + 1<<(j-1)][j-1]])
					info[i][j] = info[i][j-1];
				else 
					info[i][j] = info[i+1<<(j-1)][j-1];
				i++;
				if(i == n){
					j++;
					i = 0;
				}
		}
}

int main(){
	ios::sync_with_stdio(0);
	ifstream cin("rmq.in");
	ofstream cout("rmq.out");
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		cin >> arr[i];
	RMQ();
	for(int i = 0; i < m; i++){
			int a, b;
			cin >> a >> b;
			a--; b--;
			int lng = b - a + 1;
			int k = trunc(log2(lng));
			//cout << lng << endl;
			if(lng - (1<<k) == 0)
				cout << arr[info[a][k]] << endl;
			else
				cout << min(arr[info[a][k]], arr[info[a + lng - (1<<k)][k]]) << endl;
	}
	//for(int i = 0; i < n; i++){
	//	for(int j = 0; j < n ; j++)
	//		cout << info[i][j] << ' ';
	//	cout << endl;
	//}
	
	return(0);
}