Cod sursa(job #2657068)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 9 octombrie 2020 17:07:13
Problema Range minimum query Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

//#include <iostream>
#include <fstream>
//ifstream fin("input.in"); ofstream fout("output.out");
ifstream fin("rmq.in"); ofstream fout("rmq.out");

//VARIABLES

int v[100005];
int gr[400005];
int minn;

//FUNCTIONS

void update(int nod, int st, int dr, int pos, int val) {

    if (st == dr) {
        gr[nod] = val;
        return;
    }

    int mid = st + dr;
    mid /= 2;

    if (pos <= mid) update(nod * 2, st, mid, pos, val);
    else update(nod * 2 + 1, mid + 1, dr, pos, val);

    gr[nod] = min(gr[nod * 2], gr[nod * 2 + 1]);
}

void query(int nod, int st, int dr, int ST, int DR) {

    if (st >= ST && dr <= DR) minn = min(minn, gr[nod]);
    else {
        int mid = st + dr;
        mid /= 2;

        if (ST <= mid) query(nod * 2, st, mid, ST, DR);
        if (DR > mid) query(nod * 2 + 1, mid + 1, dr, ST, DR);
    }
    
}

//MAIN
int main() {

    int n, m;

    fin >> n >> m;
    for (int i = 1; i <= n; i++) {
        fin >> v[i];
        update(1, 1, n, i, v[i]);
    }

    for (int i = 1; i <= m; i++) {
        int a, b;
        fin >> a >> b;
        minn = 1e9;
        query(1, 1, n, a, b);
        fout << minn << '\n';
    }

    return 0;
}