Cod sursa(job #2166439)

Utilizator RazorBestPricop Razvan Marius RazorBest Data 13 martie 2018 17:07:22
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("rmq.in");
ofstream fout("rmq.out");

int n, m;
vector<int> t[100001];
int put[65];

void pre()
{
    int k = 0, i;

    for (i = 1; i <= n; i <<= 1)
    {
        put[k++] = i;
        for (int j = 0; j + i - 1 < n; j++)
            t[j].push_back(min(t[j].back(), t[j + i].back()));
    }
    put[k] = i;
}

int query(int x, int y)
{
    int k = 0, dif = y - x + 1;

    while (put[k + 1] <= dif) k++;
    return min(t[x][k], t[y - put[k] + 1][k]);
}

int main()
{
    int x, y;

    fin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        fin >> x;
        t[i].push_back(x);
    }
    t[n].push_back(0x7fffffff);

    pre();
    while (m--)
    {
        fin >> x >> y;
        fout << query(x - 1, y - 1) << '\n';
    }
}