Cod sursa(job #2637088)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 21 iulie 2020 11:07:18
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
using namespace std;

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

int n, m, x, arbint[400020];

int query(int nod, int st, int dr, int a, int b) {
    if(a <= st && dr <= b) return arbint[nod];

    int m = (st+dr)/2;
    int x = 0, y = 0;
    if(a <= m) x = query(2*nod, st, m, a, b);
    if(m < b) y = query(2*nod+1, m+1, dr, a, b);
    return max(x, y);
}

void update(int nod, int st, int dr, int poz, int val) {
    if(st == dr) {
        arbint[nod] = val;
        return;
    }

    int m = (st+dr)/2;
    if(poz <= m) update(2*nod, st, m, poz, val);
    else update(2*nod+1, m+1, dr, poz, val);

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

void citire() {
    fin >> n >> m;
    for(int i = 1; i<= n; i++) {
        fin >> x;
        update(1, 1, n, i, x);
    }
}

void solve() {
    while(m--) {
        int t, a, b;
        fin >> t >> a >> b;
        if(!t) fout << query(1, 1, n, a, b) << '\n';
        else update(1, 1, n, a, b);
    }
}

int main() {
    citire();
    solve();
}