Cod sursa(job #2805858)

Utilizator namesurname01Name Surname namesurname01 Data 22 noiembrie 2021 02:44:08
Problema Range minimum query Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>
#include <iostream>

using namespace std;
FILE* f, * g;

int arbore[1 << 18], x, a, b, poz, sol;


void query(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
        sol = min(sol, arbore[nod]);
    else
    {
        int mij = (st + dr) / 2;
        if (a <= mij)
            query(2 * nod, st, mij);
        if (b > mij)
            query(2 * nod + 1, mij + 1, dr);
    }
}
void update(int nod, int st, int dr)
{
    if (st == dr)
        arbore[nod] = x;
    else
    {
        int mij = (st + dr) / 2;
        if (poz <= mij)
            update(2 * nod, st, mij);
        else
            update(2 * nod + 1, mij + 1, dr);
        arbore[nod] = min(arbore[2 * nod], arbore[2 * nod + 1]);
    }
}
int main()
{
    f = fopen("rmq.in", "r");
    g = fopen("rmq.out", "w");
    int n, m;
    fscanf(f, "%d %d", &n, &m);
    for (int i = 1;i <= n;++i)
    {
        fscanf(f, "%d", &x);
        poz = i;
        update(1, 1, n);
    }
    for (int i = 1;i <= m;++i)
    {
        fscanf(f, "%d %d", &a, &b);
        sol = 2000000000;
        query(1, 1, n);
        fprintf(g, "%d\n", sol);
    }
    fclose(f);
    fclose(g);
    return 0;
}