Cod sursa(job #3315111)

Utilizator itachi_uchihaDarius itachi_uchiha Data 12 octombrie 2025 14:32:12
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <fstream>

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

int v[100001];
int aint[400004];
int a,b;
int maxim;

void build(int nod, int st, int dr) {
    if (st == dr) {
        aint[nod] = v[st];
        return ;
    }
    int mijloc = (st + dr) / 2;
    build(2*nod, st, mijloc);
    build(2*nod+1, mijloc+1, dr);
    aint[nod] = max(aint[nod*2], aint[nod*2+1]);
}

void update(int nod, int st, int dr) {
    if (st == dr) {
        aint[nod] = b;
        return ;
    }
    int mijloc = (st + dr) / 2;

    if (a <= mijloc)
        update(2*nod, st, mijloc);
    else
         update(2*nod+1, mijloc+1, dr);
    aint[nod] = max(aint[nod*2], aint[nod*2+1]);

}

void query(int nod, int st, int dr) {
    if (dr <= b && st >= a) {
        maxim = max(maxim, aint[nod]);
        return ;
    }
    int mijloc = (st + dr) / 2;
    if (a <= mijloc) {
        query(2*nod, st, mijloc);
    }
    if (b > mijloc) {
        query(2*nod + 1, mijloc+1, dr);
    }
}

int main () {
    int n,m;
    fin >> n >> m;


    for (int i = 1; i<=n; i++) {
        fin >> v[i];
    }

    build(1,1,n);

    int operatie;

    for (int i = 1; i<=m; i ++) {
        fin >> operatie >> a >> b;

        if (operatie == 1) {
            update(1, 1, n);
        }
        else {
            query(1,1,n);
            fout << maxim << '\n';
            maxim = -1;
        }
    }
}