Cod sursa(job #2916410)

Utilizator daniel23Malanca Daniel daniel23 Data 29 iulie 2022 16:55:42
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>

std::ifstream in("arbint.in");
std::ofstream out("arbint.out");

struct nod {
    int max, l, r;
    nod *st, *dr;
};

nod *root = new nod;
int n, m, v[100001];

void cons(nod *root, int l, int r) {
    root->l = l;
    root->r = r;
    if (l == r) {
        root->max = v[l];
    } else {
        root->st = new nod;
        root->dr = new nod;
        cons(root->st, l, (l + r) / 2);
        cons(root->dr, (l + r) / 2 + 1, r);
        root->max = std::max(root->st->max, root->dr->max);
    }
}

int max(nod *root, int l, int r) {
    if (root->r < l || root->l > r) return -1;
    if (r >= root->r && l <= root->l) return root->max;

    return std::max(max(root->st, l, r), max(root->dr, l, r));
}

void update(nod *root, int ind, int newnum) {
    if (root->l == root->r) {
        root->max = newnum;
        return;
    };

    if (ind <= (root->l + root->r) / 2)
        update(root->st, ind, newnum);
    else
        update(root->dr, ind, newnum);

    root->max = std::max(root->st->max, root->dr->max);
}

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

    cons(root, 1, n);

    for (int i = 0; i < m; i++) {
        int c, a, b;
        in >> c >> a >> b;

        if (c == 0) {
            out << max(root, a, b) << '\n';
        } else if (c == 1) {
            update(root, a, -b);
        }
    }
}