Cod sursa(job #3294997)

Utilizator Alex_at_gameIustin-Alexandru Frateanu Alex_at_game Data 1 mai 2025 12:28:18
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.91 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin fin
#define cout fout
#define inf 0x3f3f3f3f

int n, m, p, x, a, b, sol, op;
vector<int> v;

struct aint {
    vector<int> A;

    aint(int n): A(4 * n, 0) {};

    void form(int nod, int st, int dr) {
        if (st == dr) {
            A[nod] = v[st];
        } else {
            int mij = st + (dr - st) / 2;

            form(2 * nod, st, mij);
            form(2 * nod + 1, mij + 1, dr);

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

    void update(int nod, int st, int dr, int p, int x) {
        if (st == dr) {
            A[nod] = x;
        } else {
            int mij = st + (dr - st) / 2;

            if (p <= mij) {
                update(2 * nod, st, mij, p, x);
            }

            if (p > mij) {
                update(2 * nod + 1, mij + 1, dr, p, x);
            }

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

    void query(int nod, int st, int dr, int a, int b) {
        if (a <= st && dr <= b) {
            sol = max(sol, A[nod]);
        } else {
            int mij = st + (dr - st) / 2;

            if (a <= mij) {
                query(2 * nod, st, mij, a, b);
            }

            if (b > mij) {
                query(2 * nod + 1, mij + 1, dr, a, b);
            }
        }
    }
};

int main() {
    cin >> n >> m;

    v.assign(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        cin >> v[i];
    }

    aint A(n);

    A.form(1, 1, n);

    for (int i = 0; i < m; ++i) {
        cin >> op;

        if (op == 1) {
            cin >> p >> x;

            A.update(1, 1, n, p, x);
        } else {
            cin >> a >> b;

            sol = -inf;
            A.query(1, 1, n, a, b);
            cout << sol << '\n';
        }
    }
}