Cod sursa(job #3333404)

Utilizator g.vladGociu Vlad g.vlad Data 13 ianuarie 2026 11:38:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.27 kb

#include <fstream>
#include <vector>
#include <algorithm>

std::ifstream in("arbint.in");
std::ofstream out("arbint.out");
using std::vector;

unsigned N, M;

struct ArbInt {
    vector<unsigned> maxi;

    ArbInt() {
        this->maxi.resize(N << 2);
        std::fill(this->maxi.begin(), this->maxi.end(), 0);
        unsigned value;
        for(unsigned i = 1; i <= N; i += 1) {
            in >> value;
            this->write(i, value);
        }
    }

    void write(
        const unsigned target,
        const unsigned value,
        const unsigned left = 1,
        const unsigned right = N,
        const unsigned idx = 1
    ) {
        if(target == left && target == right) {
            this->maxi[idx] = value;
            return;
        }

        const unsigned middle = (left + right) / 2;

        if(target <= middle) {
            this->write(target, value, left, middle, idx * 2);
        }

        else {
            this->write(target, value, middle + 1, right, idx * 2 + 1);
        }

        this->maxi[idx] = std::max(
            this->maxi[idx * 2 + 1],
            this->maxi[idx * 2]
        );
    }

    unsigned read(
        const unsigned target_left,
        const unsigned target_right,
        const unsigned left = 1,
        const unsigned right = N,
        const unsigned idx = 1
    ) {
        if(target_left <= left && right <= target_right) {
            return this->maxi[idx];
        }

        const unsigned middle = (left + right) / 2;
        unsigned ret = 0;

        if(middle >= target_left && left <= target_right) {
            ret = this->read(target_left, target_right, left, middle, idx * 2);
        }

        if(middle <= target_right && right >= target_left) {
            ret = std::max(
                this->read(target_left, target_right, middle + 1, right, idx * 2 + 1),
                ret
            );
        }

        return ret;
    }
};

int main()
{
    in >> N >> M;
    ArbInt arb_int;

    unsigned t, x, y;
    for(;M != 0; M -= 1) {
        in >> t >> x >> y;

        if (t == 0) {
            out << arb_int.read(x, y) << '\n';
        }

        if (t == 1) {
            arb_int.write(x, y);
        }
    }

    return 0;
}