Cod sursa(job #2179126)

Utilizator DimaTCDima Trubca DimaTC Data 19 martie 2018 22:56:02
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include<bits/stdc++.h>
#define NMAX 100005
using namespace std;

int rmq[NMAX][20];
int LOG[NMAX+5],n,m,a,b;


int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ifstream cin ("rmq.in");
    ofstream cout ("rmq.out");
	cin>>n>>m;
	
	for (int i=1; i<=n; i++) cin>>rmq[i][0];
	for (int i=2; i<=NMAX; i++) LOG[i]=LOG[i>>1]+1;
	
	for (int j=1; (1<<j)<=n; j++) {
		for (int i=1; i+(1<<j)-1<=n; i++) {
			rmq[i][j]=min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
		}
	}
	
	while (m--) {
		cin>>a>>b;
		int l=b-a+1;
		int k=LOG[l];
		cout<<min(rmq[a][k], rmq[b-(1<<k)+1][k])<<'\n';
	}
	
	
	
	return 0;
}