Cod sursa(job #3228625)

Utilizator test1111fsf sfhs f hskfh 11 test1111 Data 9 mai 2024 11:21:58
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NM = 1e5 + 5;

int arb[4 * NM], a[NM], n, m, mx;

void build (int nod, int st, int dr) {
    if (st == dr) {
        arb[nod] = a[st];
    } else {
        int mij = (st + dr) / 2;
        build(2 * nod, st, mij);
        build(2 * nod + 1, mij + 1, dr);
        arb[nod] = max(arb[2 * nod + 1], arb[2 * nod]);
    }
}

void update (int nod, int st, int dr, int poz, int val) {
    if (st == dr) {
        arb[nod] = val;
    } else {
        int mij = (st + dr) / 2;
        if (poz <= mij) {
            update(2 * nod, st, mij, poz, val);
        } else {
            update(2 * nod + 1, mij + 1, dr, poz, val);
        }
        arb[nod] = max(arb[2 * nod], arb[2 * nod + 1]);
    }
}

void query (int nod, int st, int dr, int a, int b) {
    if (a <= st && dr <= b) {
        mx = max(mx, arb[nod]);
    } else {
        int mij = (st + dr) / 2;
        if (a <= mij) {
            query(2 * nod, st, mij, a, b);
        }
        if (mij + 1 <= b) {
            query(2 * nod + 1, mij + 1, dr, a, b);
        }
    }
}

int main() {
    fin >> n >> m;
    for (int i = 1; i <= n; i++) {
        fin >> a[i];
    }
    build(1, 1, n);
    while (m--) {
        int o, x, y;
        fin >> o >> x >> y;
        if (o == 1) {
            update(1, 1, n, x, y);
        } else {
            mx = 0;
            query(1, 1, n, x, y);
            fout << mx << '\n';
        }
    }
}