Cod sursa(job #3319481)

Utilizator apoputoaievladVlad Cristian Apoputoaie apoputoaievlad Data 1 noiembrie 2025 15:40:16
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2 kb
#include <fstream>

using namespace std;
ifstream cin("arbint.in");
ofstream cout("arbint.out");

const int NMAX = 1e5 + 5;

int a[NMAX];
int aint[4 * NMAX];
/*
    1 - radacina
    nod
    fiul stang: 2 * nod
    fiul drept: 2 * nod + 1
*/

// build(nod, interval de definitie)
void build(int nod, int st, int dr) { // O(N)
    if (st == dr) { // frunza
        aint[nod] = a[st];
    } else {
        int mij = (st + dr) / 2;
        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    }
}

// update(nod, interval de definitie, i, x) -> A[i] = x
// proprietate poz aparinte [st, dr]
void update(int nod, int st, int dr, int poz, int val) {
    if (st == dr) {
        aint[nod] = val;
    } else {
        int mij = (st + dr) / 2;
        if (poz <= mij) { // fiul stang
            update(2 * nod, st, mij, poz, val);
        } else {
            update(2 * nod + 1, mij + 1, dr, poz, val);
        }
        aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    }
}

// query(nod, interval de definitie, l, r) -> [l, r] intervalul de query
// proprietate [st, dr] inter. [l, r] != multimea vida
int query(int nod, int st, int dr, int qst, int qdr) {
    if (st >= qst && dr <= qdr) { // incl. compl.
        return aint[nod];
    } else {
        int mij = (st + dr) / 2, ans = 0;
        if (qst <= mij) {
            ans = max(ans, query(2 * nod, st, mij, qst, qdr));
        }
        if (mij < qdr) {
            ans = max(ans, query(2 * nod + 1, mij + 1, dr, qst, qdr));
        }
        return ans;
    }
}

int main() {
    int n, q;
    cin >> n >> q;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, 1, n);

    while (q--) {
        int type, a, b;
        cin >> type >> a >> b;

        if (type == 0) {
            cout << query(1, 1, n, a, b) << '\n';
        } else {
            update(1, 1, n, a, b);
        }
    }

    return 0;
}