Cod sursa(job #2919403)

Utilizator lucaxsofLuca Sofronie lucaxsof Data 17 august 2022 14:04:58
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <math.h>
#include <vector>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cstring>
//#include <bits/stdc++.h>

using namespace std;

ifstream fin("fileX.in");
ofstream fout("fileY.out");

typedef long long ll;

#define fast ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define maxx 100000
#define log_maxx 17

int n, Queries;

int arr[maxx];
int spares_table[maxx][log_maxx];
int log_values[maxx];

void read()
{
	fin >> n;
	fin >> Queries;
	for (int i = 0; i < n; ++i)
		fin >> arr[i];
}

void log_array()
{
	for (int i = 2; i < maxx; ++i)
		log_values[i] = log_values[i / 2] + 1;
}

void preprocess()
{
	for (int i = 0; i < n; ++i)
		spares_table[i][0] = arr[i];

	for (int j = 1; j <= log_values[n]; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i)
			spares_table[i][j] = min(spares_table[i][j - 1], spares_table[i + (1 << (j - 1))][j - 1]);
}

void query()
{
	int l, r;
	while (Queries)
	{
		Queries--;
		fin >> l >> r;
		l--, r--;
		int loggg = log_values[r - l + 1];
		int ans = min(spares_table[l][loggg], spares_table[r - (1 << loggg) + 1][loggg]);
		cout << ans << '\n';
	}
}

int main()
{
	read();
	log_array();
	preprocess();
	query();
}