Cod sursa(job #1251743)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 29 octombrie 2014 21:03:29
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>

#define Nmax 100100

using namespace std;

int N, M;

class ArbInt {

    #define Middle ((Left + Right) >> 1)
    #define leftSon (Node << 1)
    #define rightSon ((Node << 1) | 1)

    private:
        int Arb[Nmax >> 2];

    public:

        void update(int Node, int Left, int Right, int Index, int Value) {

            if(Left == Right) {
                Arb[Node] = Value;
                return;
                }

            if(Index <= Middle)
                update(leftSon, Left, Middle, Index, Value);
            else
                update(rightSon, Middle + 1, Right, Index, Value);

            Arb[Node] = max(Arb[leftSon], Arb[rightSon]);

        }

        int query(int Node, int Left, int Right, int A, int B) {

            if(A <= Left && Right <= B)
                return Arb[Node];

            int solution = 0;

            if(A <= Middle)
                solution = query(leftSon, Left, Middle, A, B);
            if(Middle < B)
                solution = max(solution, query(rightSon, Middle + 1, Right, A, B));

            return solution;

        }

} A;

void Read(ifstream & in) {

    int i, x;

    in >> N >> M;

    for(i = 1; i <= N; i++) {
        in >> x;
        A.update(1, 1, N, i, x);
        }

}
int main() {

    int type, a, b;
    ifstream in("arbint.in");
    ofstream out("arbint.out");

    Read(in);

    while(M--) {

        in >> type >> a >> b;

        if(type == 0)
            out << A.query(1, 1, N, a, b) << '\n';
        else
            A.update(1, 1, N, a, b);

        }

    in.close();
    out.close();

    return 0;

}