Cod sursa(job #2130667)

Utilizator RazorBestPricop Razvan Marius RazorBest Data 13 februarie 2018 20:09:45
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
using namespace std;

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

int t[400002];

void update(int nod, int st, int dr, int poz, int x)
{
    if (st == dr)
    {
        t[nod] = x;
        return;
    }

    int mij = (st + dr) / 2;
    if (poz <= mij)
        update(2 * nod, st, mij, poz, x);
    else
        update(2 * nod + 1, mij + 1, dr, poz, x);
    t[nod] = max(t[2 * nod], t[2 * nod + 1]);
}

int query(int nod, int st, int dr, int a, int b)
{
    if (a > dr || b < st)
        return 0;
    if (a <= st && b >= dr)
        return t[nod];

    int mij = st + (dr - st) / 2;
    if (b <= mij)
        return query(2 * nod, st, mij, a, b);
    if (a > mij)
        return query(2 * nod + 1, mij + 1, dr, a, b);
    return max(query(2 * nod, st, mij, a, b), query(2 * nod + 1, mij + 1, dr, a, b));
}

int main()
{
    int x, op, a, b, m, n;

    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        fin >> x;
        update(1, 1, n, i, x);
    }
    while (m--)
    {
        fin >> op >> a >> b;
        if (op == 0)
            fout << query(1, 1, n, a, b) << '\n';
        else
            update(1, 1, n, a, b);
    }
    return 0;
}