Cod sursa(job #3134214)

Utilizator Alex_Cristea72Cristea Alexandru Alex_Cristea72 Data 28 mai 2023 18:53:59
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream f1("arbint.in");
ofstream f2("arbint.out");

vector<int> arbore;
vector<int> vector_el;

void construieste_arbore(int node, int start, int final) {
    if (start == final) {
        arbore[node] = vector_el[start];
        return;
    }

    int mid = (start + final) / 2;
    construieste_arbore(2 * node, start, mid);
    construieste_arbore(2 * node + 1, mid + 1, final);
    arbore[node] = max(arbore[2 * node], arbore[2 * node + 1]);
}

int gaseste_maximul(int nod, int start, int final, int stanga, int dreapta) {
    if (stanga > final || dreapta < start) {
        return -1;
    }

    if (stanga <= start && dreapta >= final) {
        return arbore[nod];
    }

    int mid = (start + final) / 2;
    int max_stanga = gaseste_maximul(2 * nod, start, mid, stanga, dreapta);
    int max_dreapta = gaseste_maximul(2 * nod + 1, mid + 1, final, stanga, dreapta);

    return max(max_stanga, max_dreapta);
}

void update_valoare(int nod, int start, int final, int index, int valoare) {
    if (start == final) {
        arbore[nod] = valoare;
        return;
    }

    int mijloc = (start + final) / 2;
    if (index <= mijloc) {
        update_valoare(2 * nod, start, mijloc, index, valoare);
    } else {
        update_valoare(2 * nod + 1, mijloc + 1, final, index, valoare);
    }
    arbore[nod] = std::max(arbore[2 * nod], arbore[2 * nod + 1]);
}

int main() {
    int N, M;
    f1 >> N >> M;
    vector_el.resize(N);
    for (int i = 0; i < N; i++) {
        f1 >> vector_el[i];
    }

    int marime_arbore = 40003;
    arbore.resize(marime_arbore);

    construieste_arbore(1, 0, N - 1);

    while (M--) {
        int tip, a, b;
        f1 >> tip >> a >> b;

        if (tip == 0) {
            int val_max = gaseste_maximul(1, 0, N - 1, a - 1, b - 1);
            f2 << val_max << "\n";
        } else if (tip == 1) {
            update_valoare(1, 0, N - 1, a - 1, b);
        }
    }



    return 0;
}