Cod sursa(job #2900635)

Utilizator RobertuRobert Udrea Robertu Data 11 mai 2022 17:08:54
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");

const int dim = 1e5 + 2;
int arb[4*dim], maxim;

int getMaxim(int nod, int left, int right, int pozS, int pozD) {
    if(pozS > right || pozD < left) return -1;

    if( pozS <= left && right <= pozD ) return arb[nod];

    int mij = (left + right) / 2;

    return max(getMaxim(nod * 2, left, mij, pozS, pozD), getMaxim(nod * 2 + 1, mij + 1, right,  pozS, pozD));
}

void updateElement(int nod, int left, int right, int poz, int val) {
    if(left == right) {
        arb[nod] = val;
        return;
    }

    int mij = (left + right) / 2;

    if( mij >= poz )
        updateElement(nod * 2, left, mij, poz, val);
    else 
        updateElement(nod * 2 + 1, mij + 1, right, poz, val);

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

int main() {
    int n, m, op, a, b;

    fin >> n >> m;

    for(int i = 1; i <= n; i++) {
        fin >> a;
        updateElement(1, 1, n, i, a);
    }

    for(int i = 0; i < m; i++) {
        fin >> op >> a >> b;

        if(op == 0) 
            fout << getMaxim(1, 1, n, a, b) << '\n';
        else 
            updateElement(1, 1, n, a, b);
    }

    return 0;
}