Cod sursa(job #3357736)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 13:29:18
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int N = 100010;
int A[N];
int tree[4 * N];

void build(int node, int start, int end) {
    if (start == end) {
        tree[node] = A[start];
    } else {
        int mid = (start + end) / 2;
        build(2 * node, start, mid);
        build(2 * node + 1, mid + 1, end);
        tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    }
}

void update(int node, int start, int end, int idx, int val) {
    if (start == end) {
        A[idx] = val;
        tree[node] = val;
    } else {
        int mid = (start + end) / 2;
        if (idx >= start && idx <= mid) {
            update(2 * node, start, mid, idx, val);
        } else {
            update(2 * node + 1, mid + 1, end, idx, val);
        }
        tree[node] = max(tree[2 * node], tree[2 * node + 1]);
    }
}

int query(int node, int start, int end, int l, int r) {
    if (r < start || end < l) {
        return 0;
    }
    if (l <= start && end <= r) {
        return tree[node];
    }
    int mid = (start + end) / 2;
    int left_query = query(2 * node, start, mid, l, r);
    int right_query = query(2 * node + 1, mid + 1, end, l, r);
    return max(left_query, right_query);
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
    }
    build(1, 1, n);
    for (int i = 1; i <= m; i++) {
        int x, a, b;
        cin >> x >> a >> b;
        if (x == 0) {
            cout << query(1, 1, n, a, b) << '\n';
        } else {
            update(1, 1, n, a, b);
        }
    }
    return 0;
}