Cod sursa(job #2563836)

Utilizator IoLeoLeonard Rumeghea IoLeo Data 1 martie 2020 15:06:54
Problema Range minimum query Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
#include <set>
using namespace std;

set<int> ListaVecini[256];

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

int n, m, vect[256], mat[256][256];

void GenerareMatrice(){

    for(int i = 1; i <= n; i++) mat[i][0] = i;

    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i <= n; i++){
            if(vect[mat[i][j - 1]] < vect[mat[i + (1 << (j - 1))][j - 1]]){

                mat[i][j] = mat[i][j - 1];
            }
            else mat[i][j] = mat[i + (1 << (j - 1))][j - 1];
        }
    }
}

int Interogare(int x, int y){
    int d  = y - x + 1;

    int d1 = d;
    int c = 0;

    while(d1) d1 /= 2, c++;

    c--;

    return min(vect[mat[x][c]], vect[mat[x + d - (1 << c)][c]]);
}

int main(){

    in >> n >> m;

    for(int i = 1; i <= n; i++) in >> vect[i];

    GenerareMatrice();

    /*for(int i = 1; i <= n; i++){
        for(int j = 0; j <= n; j++) cout << vect[mat[i][j]] << " ";
        cout << endl;
    }

    cout << endl;

    int x, y;

    for(int  i = 1; i <= n; i++) cout << i << " ";
    cout << endl;
    for(int  i = 1; i <= n; i++) cout << vect[i] << " ";
    cout << endl << endl;

    while(in >> x >> y){
        cout << x << " - " << y << " => " << Interogare(x, y) << endl;
    }*/

    int x, y;

    for(int i = 1; i <= m; i++){
        in >> x >> y;
        out << Interogare(x, y) << endl;
    }

}