Cod sursa(job #2234684)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 25 august 2018 22:52:20
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
//#include <bits/stdc++.h>
#include <fstream>
#include <vector>
#include <math.h>
 
using namespace std;
 
/********** TEMPLATE STARTS HERE ***********/
 
#define IOS ios::sync_with_stdio(false), cin.tie(0);
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define pb push_back
#define test int t; cin >> t; while(t--)
#define skip continue
#define stop break
#define sz(v) v.size()
#define endl '\n'
#define PI 3.1415926535897932384626433832795
#define EPS 1e-9
#define FR(i, l, r) for(int i = (l); i <= (r); i++)
#define gcd __gcd 
#define FO(i, a) for(auto & i : a)
#define debug(a) cout << #a << ": " << a << endl
#define debug1(a, l, r) FR(i, l, r) cout << a[i] << " "; cout << endl
#define SET(a, b) memset(a, b, sizeof(a));
#define refresh fflush(stdout);
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef vector <pll> vll;
 
const int INF = 0x3f3f3f3f;
const int LINF = 0x3f3f3f3f3f3f3f3f;
 
template <typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template <typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
//template <typename T> inline void out(T x) { cout << x; }
 
/*********** TEMPLATE ENDS HERE *************/
 
ifstream cin ("rmq.in");
ofstream cout("rmq.out");
 
int a[18][100007];
int n;
 
void build_table()
{
    for(int i = 1; i <= n; i++)
        cin >> a[0][i];
     
    for(int i = 1; i <= log(n) + 1; i++)
	{
		int p = 1 << (i - 1);
        for(int j = 1; j <= n - p; j++)
            a[i][j] = min(a[i - 1][j], a[i - 1][j + p]);
	}            
}
 
main()
{
    int q;
    cin >> n >> q;
    build_table();
    while(q--)
    {
		int x, y;
		cin >> x >> y;
		int e = log(y - x + 1);
		cout << min(a[e][x], a[e][y - (1 << e) + 1]) << '\n';
    }
}