Cod sursa(job #2767495)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 6 august 2021 13:49:50
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <cmath>

int aint[400005];

int querry(int left, int right, int nod, int ileft, int iright) {
    if (left == ileft && right == iright) {
        return aint[nod];
    }
    else {
        int mid = (left + right) / 2, ans = 0;
        if (ileft <= mid) {
            ans = querry(left, mid, 2 * nod, ileft, std::min(mid, iright));
        }
        if (mid < iright) {
            ans = std::max(ans, querry(mid + 1, right, 2 * nod + 1, std::max(mid + 1, ileft), iright));
        }
        return ans;
    }
}

void update(int left, int right, int nod, int pos, int val) {
    if (left == right) {
        aint[nod] = val;
    }
    else {
        int mid = (left + right) / 2;
        if (pos <= mid) {
            update(left, mid, 2 * nod, pos, val);
        }
        else {
            update(mid + 1, right, 2 * nod + 1, pos, val);;
        }
        aint[nod] = std::max(aint[2 * nod], aint[2 * nod + 1]);
    }
}

int main()
{
    std::ifstream fin("arbint.in");
    std::ofstream fout("arbint.out");
    int nrn, nrm, nri, cer, left, right, pos, val;
    fin >> nrn >> nrm;
    for (int index = 1; index <= nrn; index++) {
        fin >> nri;
        update(1, nrn, 1, index, nri);
    }
    for (int index = 0; index < nrm; index++) {
        fin >> cer;
        if (cer) {
            fin >> pos >> val;
            update(1, nrn, 1, pos, val);
        }
        else {
            fin >> left >> right;
            fout << querry(1, nrn, 1, left, right) << '/n';
        }
    }
}