Cod sursa(job #3288960)

Utilizator mihai_bosIancu Mihai mihai_bos Data 24 martie 2025 23:16:13
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

const int MAX_SIZE = 1e5 + 5;

int n, q;
vector<int> valori(MAX_SIZE);
vector<int> arbore(4 * MAX_SIZE);

void build(int nod, int st, int dr) {
    if (st == dr) {
        arbore[nod] = valori[st];
    } else {
        int mij = (st + dr) / 2;
        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        arbore[nod] = max(arbore[2 * nod], arbore[2 * nod + 1]);
    }
}

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

    int mij = (st + dr) / 2;
    int maxSt = 0, maxDr = 0;

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

    return max(maxSt, maxDr);
}

void update(int nod, int st, int dr, int poz, int valoareNoua) {
    if (st == dr) {
        arbore[nod] = valoareNoua;
    } else {
        int mij = (st + dr) / 2;
        if (poz <= mij)
            update(2 * nod, st, mij, poz, valoareNoua);
        else
            update(2 * nod + 1, mij + 1, dr, poz, valoareNoua);

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

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

    for (int i = 1; i <= n; i++) {
        fin >> valori[i];
    }

    build(1, 1, n);

    for (int i = 0; i < q; i++) {
        int t, x, y;
        fin >> t >> x >> y;

        if (t == 0)
            fout << query(1, 1, n, x, y) << '\n';
        else
           update(1, 1, n, x, y);
    }

    return 0;
}