Cod sursa(job #1378371)

Utilizator diana97Diana Ghinea diana97 Data 6 martie 2015 11:52:09
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f ("arbint.in");
ofstream g ("arbint.out");

const int NMAX = 100000;

int n, m;
int arb[4 * NMAX + 1];

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

int query(int nod, int st, int dr, int a, int b) {
    if (a <= st && dr <= b) return arb[nod];
    int m = (st + dr) / 2, sol = -1;
    if (a <= m) sol = max(sol, query(2 * nod, st, m, a, b));
    if (m < b) sol = max(sol, query(2 * nod + 1, m + 1, dr, a, b));
    return sol;
}

void citeste() {
    int x;
    f >> n >> m;
    for (int i = 1; i <= n; i++) {
        f >> x;
        update(1, 1, n, i, x);
    }
}

void rezolva() {
    int tip, a, b;
    for (int i = 1; i <= m; i++) {
        f >> tip >> a >> b;
        if (tip == 0) g << query(1, 1, n, a, b) << '\n';
        else update(1, 1, n, a, b);
    }
}

int main() {
    citeste();
    rezolva();
    return 0;
}