Cod sursa(job #3300283)

Utilizator VramzVramita Darius Adrian Vramz Data 14 iunie 2025 15:00:05
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

const int MAXN = 400005;
long long arbore[MAXN];

void modificaNod(int nod, int st, int dr, int pozitie, long long valoare) {
    if (st == dr) {
        arbore[nod] = valoare;
        return;
    }

    int mijloc = (st + dr) / 2;
    if (pozitie <= mijloc)
        modificaNod(2 * nod, st, mijloc, pozitie, valoare);
    else
        modificaNod(2 * nod + 1, mijloc + 1, dr, pozitie, valoare);

    arbore[nod] = max(arbore[2 * nod], arbore[2 * nod + 1]);
}

long long maximInterval(int nod, int st, int dr, int left, int right) {
    if (left <= st && dr <= right)
        return arbore[nod];

    int mijloc = (st + dr) / 2;
    long long rezultat = 0;

    if (left <= mijloc)
        rezultat = max(rezultat, maximInterval(2 * nod, st, mijloc, left, right));
    if (mijloc < right)
        rezultat = max(rezultat, maximInterval(2 * nod + 1, mijloc + 1, dr, left, right));

    return rezultat;
}

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

    for (int i = 1; i <= n; ++i) {
        long long val;
        fin >> val;
        modificaNod(1, 1, n, i, val);
    }

    while (q--) {
        int tip, a, b;
        fin >> tip >> a >> b;
        if (tip == 1) {
            modificaNod(1, 1, n, a, b);
        } else {
            fout << maximInterval(1, 1, n, a, b) << '\n';
        }
    }

    return 0;
}