Cod sursa(job #2551813)

Utilizator CosminMorarMorar Cosmin Andrei CosminMorar Data 20 februarie 2020 11:10:02
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int n, q;
int maxValue[400010];

void act(int nod, int st, int dr, int pos, int value) {
    if (st == dr) {
        maxValue[nod] = value;
        return;
    }

    int mij = (st + dr) / 2;
    if (pos <= mij)
        act(2 * nod, st, mij, pos, value);
    else
        act(2 * nod + 1, mij + 1, dr, pos, value);

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

int ask(int nod, int st, int dr, int a, int b) {
    if (a <= st && dr <= b)
        return maxValue[nod];

    int mij = (st + dr) / 2;
    int maxSt = -1, maxDr = -1;
    if (a <= mij)
        maxSt = ask(2 * nod, st, mij, a, b);
    if (mij + 1 <= b)
        maxDr = ask(2 * nod + 1, mij + 1, dr, a, b);

    return max(maxSt, maxDr);
}

void readAndSet() {
    fin >> n >> q;
    for (int i = 1; i <= n; i++) {
        int value;
        fin >> value;
        act(1, 1, n, i, value);
    }
}

void solveQueries() {
    for (int i = 1; i <= q; i++) {
        int t, a, b;
        fin >> t >> a >> b;
        if (t == 1)
            act(1, 1, n, a, b);
        else
            fout << ask(1, 1, n, a, b) << '\n';
    }
}

int main() {
    readAndSet();
    solveQueries();
    return 0;
}