Cod sursa(job #1267724)

Utilizator diana97Diana Ghinea diana97 Data 20 noiembrie 2014 10:19:00
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 100000 + 1;

int n, m, poz, valoare, a, b, sol;
int arb[4 * NMAX];

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

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

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

void rezolva() {
    int tip;
    while (m--) {
        f >> tip;
        if (tip == 0) {
            sol = -1;
            f >> a >> b;
            query(1, 1, n);
            g << sol << '\n';
        }
        else {
            f >> poz >> valoare;
            update(1, 1, n);
        }
    }
}

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