Cod sursa(job #3279373)

Utilizator mihaihvhTuburlui Mihai mihaihvh Data 22 februarie 2025 19:01:52
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <vector>
#include <climits>

using namespace std;

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

vector<int> aint;
int n, m;

int mmax(int poz, int st, int dr, int stI, int drI) {
    if (dr < stI || st > drI) return INT_MIN;
    if (stI <= st && dr <= drI) return aint[poz];
    int mmax1 = mmax(poz * 2, st, (st + dr) / 2, stI, drI);
    int mmax2 = mmax(poz * 2 + 1, (st + dr) / 2 + 1, dr, stI, drI);
    if (mmax1 > mmax2) return mmax1;
    else return mmax2;
}

void update(int st, int dr, int poz, int pozu, int val) {
    if (pozu > dr || pozu < st) return;
    if (st == dr) {
        aint[poz] = val;
        return;
    }
    update(st, (st + dr) / 2, poz * 2, pozu, val);
    update((st + dr) / 2 + 1, dr, poz * 2 + 1, pozu, val);
    int mmax1 = aint[poz * 2];
    int mmax2 = aint[poz * 2 + 1];
    if (mmax1 > mmax2) aint[poz] = mmax1;
    else aint[poz] = mmax2;
}

int main() {
    cin >> n >> m;
    aint.resize(4*n);
    for (int in, i = 1; i <= n; ++i) {
        cin >> in;
        update(1, n, 1, i, in);
    }
    for (int op, x, y, i = 1; i <= m; ++i) {
        cin >> op >> x >> y;
        if (!op) cout << mmax(1, 1, n, x, y) << '\n';
        else update(1, n, 1, x, y);
    }
    return 0;
}