Cod sursa(job #1059927)

Utilizator DanielSanduSandu Daniel DanielSandu Data 17 decembrie 2013 11:17:48
Problema Arbori de intervale Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
#include <iostream>

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

int v[500000];
size_t n;

void create(size_t node, size_t lhs, size_t rhs) {
    if (lhs == rhs) {
        in>>v[node];
        return;
    }
    create(node * 2, lhs, (lhs + rhs) / 2);
    create(node * 2 + 1, (lhs + rhs) / 2 + 1, rhs);
    v[node] = std::max(v[node * 2], v[node * 2 + 1]);
}

int max_arb(size_t node, size_t a, size_t b, size_t lhs, size_t rhs) {
    if (a <= lhs && rhs <= b)
        return v[node];
    size_t mid = (lhs + rhs) / 2;
    if (b <= mid)
        return max_arb(node * 2, a, b, lhs, mid);
    if (a >= mid + 1)
        return max_arb(node * 2 + 1, a, b, mid + 1, rhs);
    return std::max(max_arb(node * 2, a, b, lhs, mid), max_arb(node * 2 + 1, a, b, mid + 1, rhs));
}

void modify(int val, size_t pos, size_t node, size_t lhs, size_t rhs) {
    if (val > v[node])
        v[node] = val;
    if (lhs == rhs)
        v[node] = val;
    else {
        size_t mid = (lhs + rhs) / 2;
        if (pos <= mid)
            modify(val, pos, node * 2, lhs, mid);
        else
            modify(val, pos, node * 2 + 1, mid + 1, rhs);
    }
}

int main() {
    size_t m, o, i, j;
    in>>n>>m;
    create(1, 1, n);
    while (in>>o>>i>>j)
        if (o == 0)
            out<<max_arb(1, i, j, 1, n)<<"\n";
        else
            modify(j, i, 1, 1, n);
    return 0;
}