Cod sursa(job #3356155)

Utilizator rapidu36Victor Manz rapidu36 Data 29 mai 2026 19:14:22
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>
#include <vector>

using namespace std;

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

vector <int> aint;

int pow_2(int n) {
    int p = 1;
    while (p <= n) {
        p *= 2;
    }
    return p * 2;
}

void constructie_aint(int p, int st, int dr) {
    if (st == dr) {
        in >> aint[p];
        return;
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    constructie_aint(fs, st, m);
    constructie_aint(fd, m + 1, dr);
    aint[p] = max(aint[fs], aint[fd]);
}

int interogare(int p, int st, int dr, int a, int b) {
    if (a <= st && dr <= b) {
        return aint[p];
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    int rez_st = -1, rez_dr = -1;
    if (a <= m) {
        rez_st = interogare(fs, st, m, a, b);
    }
    if (m + 1 <= b) {
        rez_dr = interogare(fd, m + 1, dr, a, b);
    }
    return max(rez_st, rez_dr);
}

void actualizare(int p, int st, int dr, int poz, int val) {
    if (st == dr) {
        aint[p] = val;
        return;
    }
    int m = (st + dr) / 2, fs = 2 * p, fd = 2 * p + 1;
    if (poz <= m) {
        actualizare(fs, st, m, poz, val);
    } else {
        actualizare(fd, m + 1, dr, poz, val);
    }
    aint[p] = max(aint[fs], aint[fd]);
}

int main() {
    int n, q;
    in >> n >> q;
    aint.resize(pow_2(n));
    constructie_aint(1, 1, n);
    for (int i = 0; i < q; i++) {
        int tip;
        in >> tip;
        if (tip == 0) {
            int a, b;
            in >> a >> b;
            out << interogare(1, 1, n, a, b) << "\n";
        } else {
            int poz, val;
            in >> poz >> val;
            actualizare(1, 1, n, poz, val);
        }
    }
    return 0;
}