Cod sursa(job #1413340)

Utilizator Alexghita96Ghita Alexandru Alexghita96 Data 1 aprilie 2015 20:20:21
Problema Range minimum query Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <algorithm>

#define NMAX 100005
#define INF 0x3f3f3f3f

using namespace std;

int N, M, arb[4 * NMAX], a, b, minim;

void update(int nod, int st, int dr)
{
    if (st == dr)
    {
        arb[nod] = b;
        return;
    }

    int mij = (st + dr) / 2;

    if (a <= mij)
        update(2 * nod, st, mij);
    else
        update(2 * nod + 1, mij + 1, dr);
    arb[nod] = min(arb[2 * nod], arb[2 * nod + 1]);
}

void query(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
    {
        minim = min(minim, arb[nod]);
        return;
    }

    int mij = (st + dr) / 2;

    if (a <= mij)
        query(2 * nod, st, mij);
    if (mij < b)
        query(2 * nod + 1, mij + 1, dr);
}

void citire()
{
    scanf("%d %d", &N, &M);

    for (a = 1; a <= N; ++a)
    {
        scanf("%d", &b);
        update(1, 1, N);
    }
}

void rezolvare()
{
    for (int i = 1; i <= M; ++i)
    {
        scanf("%d %d", &a, &b);
        minim = INF;
        query(1, 1, N);
        printf("%d\n", minim);
    }
}

int main()
{
    freopen("rmq.in", "r", stdin);
    freopen("rmq.out", "w", stdout);

    citire();
    rezolvare();

    return 0;
}