Cod sursa(job #2331621)

Utilizator andra_moldovanAndra Moldovan andra_moldovan Data 29 ianuarie 2019 19:09:04
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>

#define MAXN 100005

using namespace std;

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

int ait[MAXN * 4], N;

inline void Update(int poz, int ls, int ld, int p, int val) {
    if (ls == ld && ls == p) {
        ait[poz] = val;
        return;
    }
    if (ls > p || ld < p)
        return;
    int mij = (ls + ld) / 2;

    Update(poz * 2, ls, mij, p, val);
    Update(poz * 2 + 1, mij + 1, ld, p, val);

    ait[poz] = max(ait[poz * 2], ait[poz * 2 + 1]);
}

inline int Query(int poz, int ls, int ld, int st, int dr) {
    if (st <= ls && ld <= dr)
        return ait[poz];
    if (st > ld || dr < ls)
        return 0;
    int mij = (ls + ld) / 2;

    int aux1 = Query(poz * 2, ls, mij, st, dr);
    int aux2 = Query(poz * 2 + 1, mij + 1, ld, st, dr);

    return max(aux1, aux2);
}

inline void Read() {
    int M, a, b, tip;

    fin >> N >> M;

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

        Update(1, 1, N, i, a);
    }

    while (M--){
        fin >> tip >> a >> b;

        if (tip == 0) {
            fout << Query(1, 1, N, a, b) << "\n";
        }
        else {
            Update(1, 1, N, a, b);
        }
    }
}

int main () {
    Read();

    fin.close(); fout.close(); return 0;
}