Cod sursa(job #3149426)

Utilizator TeddyDinutaDinuta Eduard Stefan TeddyDinuta Data 8 septembrie 2023 15:24:55
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");
int t[400005], n, m, a[100005];
int type, l, r;

void build(int nod, int l, int r) {
    if (l == r) {
        t[nod] = a[l];
    } else {
        int m = (l + r) / 2;
        build(2 * nod, l, m);
        build(2 * nod + 1, m + 1, r);
        t[nod] = max(t[2 * nod], t[2 * nod + 1]);
    }
}

int query(int nod, int l, int r, int x, int y) {
    if (l > y || r < x)
        return 0;
    if (l >= x && r <= y)
        return t[nod];

    int m = (l + r) / 2;
    return max(query(2 * nod, l, m, x, y), query(2 * nod + 1, m + 1, r, x, y));
}

void update(int nod, int l, int r, int pos, int val) {
    if (l == r) {
        t[nod] = val;
    } else {
        int m = (l + r) / 2;
        if (pos <= m) {
            update(2 * nod, l, m, pos, val);
        } else {
            update(2 * nod + 1, m + 1, r, pos, val);
        }

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

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    in >> n >> m;
    for (int i = 1; i <= n; i++) {
        in >> a[i];
    }
    build(1, 1, n);

    while (m--) {
        in >> type >> l >> r;
        if (type == 0) {
            out << query(1, 1, n, l, r) << '\n';
        } else {
            update(1, 1, n, l, r);
        }
    }

    return 0;
}