Cod sursa(job #2763036)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 11 iulie 2021 10:24:40
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
using namespace std;

const int MAXN =  5 * 1e5 + 65;
const int INF = 1e8;

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

int n, aint[MAXN], ans;

void query(int nod, int st, int dr, int left, int right){

    if(st >= left and dr <= right){
        ans = max(ans, aint[nod]);
        return;
    }
    int m = (st + dr) / 2 ;

    if(m >= left)  query(2 * nod, st, m, left, right);
    if(m < right)   query(2 * nod + 1, m + 1, dr, left, right);

}

void update(int nod, int st, int dr, int poz, int number){

    if(dr - st <= 0) {
        aint[nod] = number;
        return;
    }
    int m = (st + dr) / 2 ;

    if(poz > m)     update(2 * nod + 1, m + 1, dr, poz, number);
    else if(poz <= m)    update(2 * nod, st, m, poz, number);

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

}

int main(){

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

   for(int i = 1; i <= m; ++i){
        int type, x, y; fin >> type >> x >> y;
        if(type == 0){
            ans = 0;
            query(1, 1, n, x, y);
            fout << ans << '\n';
        }
        else update(1, 1, n, x, y);
    }
    return 0;
}