Cod sursa(job #3208759)

Utilizator teodora_lauraTeodora teodora_laura Data 29 februarie 2024 20:57:56
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

const int N = 100005;
const int logN = 18;
int n, m;
int mat[logN][N];
int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> mat[0][i];
	for (int i = 1; i <= logN; i++)
		for (int j = 1; j <= n + 1 - (1 << i); j++)
			mat[i][j] = min(mat[i - 1][j], mat[i - 1][j + (1 << (i - 1))]);
	
	
	for (int i = 1; i <= m; i++)
	{
		int st, dr;
		cin >> st >> dr;
		int nr = 1, putere=0;
		while(dr-st<nr)
		{
			putere++;
			nr *= 2;
		}
		cout <<"======>" << min(mat[putere][st], mat[putere][dr - (1 << putere)+1]) << "\n";

	}
	return 0;
}
/*
1 2 3 4 5 6 7 8 9 10
4 5 9 8 7 5 3 1 5 7
4 5 8 7 5 3 1 1 5
4 5 5 3 1 1 1
1 1 1
*/