Cod sursa(job #1098124)

Utilizator Theorytheo .c Theory Data 4 februarie 2014 15:01:46
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <fstream>
#include <cassert>

using namespace std;

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


int N; int M; int max_value;


class Arbore {

    static const int NMAX = 100009;;

    private: int Arb[NMAX * 2];

    public :

    int max(int X, int Y){ if(X > Y) return X; else return Y;}

    int left(const int &nod) {
        return nod * 2;
    }

    int right(const int &nod){
        return nod * 2 + 1;
    }

    void update(int nod, int st, int dr, const int &pos, const int &value) {

        if(st == dr) {Arb[nod] = value; }
        else {
            int mid = (st + dr) / 2;
            if(pos <= mid)
                update(left(nod), st, mid, pos, value);
                    else
                update(right(nod), mid + 1, dr, pos, value);
            if(right(nod) < 2 * NMAX)
            Arb[nod] = max(Arb[left(nod)], Arb[right(nod)]);

        }
    }

    void query(int nod, int st, int dr, const int &pos_st, const int &pos_dr) {

        if(pos_st <= st && dr <= pos_dr)
            max_value = max(max_value, Arb[nod]);
                else {
            int mid = (st + dr) / 2;

            if(pos_st <= mid) query(left(nod), st, mid, pos_st, pos_dr);

            if(mid  < pos_dr) query(right(nod), mid + 1, dr, pos_st, pos_dr);
        }

    }
} A;

int main() {

    fin >> N >> M;

    assert(N <= 100000 && M <= 100000);

    for(int i = 1; i <= N; ++i) {

        int value; fin >> value;
        A.update(1, 1, N, i, value);
    }

    while( M-- ) {

        int type;int a; int b ; fin >> type; fin >> a >> b;

        if(type == 1) A.update(1 , 1 , N, a , b);
        if(type == 0) {max_value = 0; A.query(1 , 1, N, a, b); fout << max_value <<'\n';}

    }



    return 0;
}