Cod sursa(job #3298559)

Utilizator mateicrainiceanuMatei Crainiceanu mateicrainiceanu Data 31 mai 2025 11:19:32
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <fstream>
using namespace std;

#define MAX_SIZE 100001

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

int n, q;
int arbore[4 * MAX_SIZE];
    int queryLeft, queryRight, value, idx, result;

inline int max(int a, int b) {
    return (a > b) ? a : b;
}

void update(int nod, int l, int r) {
    if (l == r) {
        arbore[nod] = value;
        return;
    }

    int mid = (l + r) / 2;
    if (idx <= mid)
        update(2 * nod, l, mid);
    else
        update(2 * nod + 1, mid + 1, r);

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

// Query maximum in range [queryLeft, queryRight]
void query(int nod, int l, int r) {
    if (queryLeft <= l && r <= queryRight) {
        result = max(result, arbore[nod]);
        return;
    }

    int mid = (l + r) / 2;
        if (queryLeft <= mid)
        query(2 * nod, l, mid);
        if (mid < queryRight)
        query(2 * nod + 1, mid + 1, r);
}

int main() {

    fin >> n >> q;

    //constructia arborelui
    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        idx = i;
        value = x;
        update(1, 1, n);
    }

    // rezolvare query
    for (int i = 0; i < q; ++i) {
        int op, a, b;
        fin >> op >> a >> b;

        if (op == 0) {
            queryLeft = a;
            queryRight = b;
            result = -1;
            query(1, 1, n);
            fout<<result<<"\n";
        } else {
            idx = a;
            value = b;
            update(1, 1, n);
        }
    }

    return 0;
}