Cod sursa(job #3208823)

Utilizator teodora_lauraTeodora teodora_laura Data 1 martie 2024 10:15:06
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
const int N = 100005;
const int logN = 18;
int n, m;
int mat[logN][N], logaritm[N];
int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	f >> n >> m;
	for (int i = 1; i <= n; i++)
		f >> 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))]);
	logaritm[1] = 0;
	int k = 0;
	for(int i=2;i<=n;i++)
	{
		logaritm[i] = logaritm[i - (1 << k)] + 1;
			k++;
	}
	for (int i = 1; i <= m; i++)
	{
		int st, dr;
		f >> st >> dr;
		int putere = logaritm[dr-st];
		g << 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
*/