Cod sursa(job #2944189)

Utilizator Luca07Nicolae Luca Luca07 Data 22 noiembrie 2022 10:10:34
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
//#include <iostream>
#include <fstream>
#include<vector>
using namespace std;

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

vector<int> st;

void update(int node, int from, int to, int pos, int val) {
    if (from == to) {
        st[node] = val;
        return;
    }

    int mid = (from + to) / 2;
    if (pos <= mid) {
        update(node * 2, from, mid, pos, val);
    }
    else {
        update(node * 2 + 1, mid + 1, to, pos, val);
    }
    st[node] = max(st[node * 2], st[node * 2 + 1]);

}

int query(int node, int from, int to, int ql, int qr) {
    int smax = 0;
    if (ql <= from && to <= qr) {
        return st[node];
    }

    int mid = (from + to) / 2;

    if (ql <= mid) {
        smax = max(smax, query(node * 2, from, mid, ql, qr));
    }
    if (mid + 1 <= qr) {
        smax = max(smax, query(node * 2 + 1, mid + 1, to, ql, qr));
    }
    return smax;
}

int main()
{
    int n, m, i, nr,a,b ,c;

    cin >> n >> m;
    st.resize(4 * n + 5);

    for (i = 1; i <= n; i++) {
        cin >> nr;
        update(1, 1, n, i, nr);
    }

    for (i = 0; i < m; i++) {
        cin >> a >> b >> c;
        if (a == 0) {
            cout << query(1, 1, n, b, c)<<"\n";
        }
        else {
            update(1, 1, n, b, c);
        }
    }


    
    return 0;
}