Cod sursa(job #3135322)

Utilizator florinilie324Ilie Florin Alexandru florinilie324 Data 2 iunie 2023 17:45:16
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 100000;
int arbore[4*MAXN];
int N, M, a, b, op;

void build(int nod, int capatSt, int capatDr) {
    if (capatSt == capatDr) {
        scanf("%d", &arbore[nod]);
    } else {
        int mij = (capatSt + capatDr) / 2;
        build(nod*2, capatSt, mij);
        build(nod*2+1, mij+1, capatDr);
        arbore[nod] = max(arbore[nod*2], arbore[nod*2+1]);
    }
}

int getMaxim(int nod, int capatSt, int capatDr, int st, int dr) {
    if (st > dr)
        return -1;
    if (st == capatSt && dr == capatDr)
        return arbore[nod];
    int mij = (capatSt + capatDr) / 2;
    return max(getMaxim(nod*2, capatSt, mij, st, min(dr, mij)),
               getMaxim(nod*2+1, mij+1, capatDr, max(st, mij+1), dr));
}

void update(int nod, int capatSt, int capatDr, int pozitie, int valoare_noua) {
    if (capatSt == capatDr) {
        arbore[nod] = valoare_noua;
    } else {
        int mij = (capatSt + capatDr) / 2;
        if (pozitie <= mij)
            update(nod*2, capatSt, mij, pozitie, valoare_noua);
        else
            update(nod*2+1, mij+1, capatDr, pozitie, valoare_noua);
        arbore[nod] = max(arbore[nod*2], arbore[nod*2+1]);
    }
}

int main() {
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    scanf("%d%d", &N, &M);
    build(1, 1, N);
    for(int i = 0; i < M; i++) {
        scanf("%d%d%d", &op, &a, &b);
        if(op == 0) {
            printf("%d\n", getMaxim(1, 1, N, a, b));
        } else if(op == 1) {
            update(1, 1, N, a, b);
        }
    }

    return 0;
}