Cod sursa(job #567618)

Utilizator toniobFMI - Barbalau Antonio toniob Data 30 martie 2011 11:40:44
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
using namespace std;

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

const int N = 100000;
int n, rez, arb[N * 5 + 5], m;

inline int max (int a, int b) {
    return a > b ? a : b;
}

void mod (int nod, int p, int u, int poz, int key) {
    if (p == u) {
        arb[nod] = key;
        return;
    }

    int mij = (p + u) / 2;

    if (poz <= mij) {
        mod (nod * 2, p, mij, poz, key);
    } else {
        mod (nod * 2 + 1, mij + 1, u, poz, key);
    }

    arb[nod] = max (arb[nod * 2], arb[nod * 2 + 1]);
}

void citire () {
    in >> n >> m;
    for (int i = 1, x; i <= n; ++i) {
        in >> x;
        mod (1, 1, n, i, x);
    }
}

void cauta (int nod, int p, int u, int x, int y) {
    if (x <= p && u <= y) {
        rez = max (rez, arb[nod]);
        return;
    }

    int mij = (p + u) / 2;
    if (x <= mij) {
        cauta (nod * 2, p, mij, x, y);
    }
    if (y > mij) {
        cauta (nod * 2 + 1, mij + 1, u, x, y);
    }
}

void exe () {
    for (int i = 1, c, a, b; i <= m; ++i) {
        in >> c >> a >> b;
        if (c == 1) {
            mod (1, 1, n, a, b);
            continue;
        }
        if (c == 0) {
            rez = -1;
            cauta (1, 1, n, a, b);
            out << rez << '\n';
        }
    }
}

int main () {
    citire ();

    exe ();

    return 0;
}