Cod sursa(job #1767989)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 29 septembrie 2016 23:14:33
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

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

const int dim = 100005;

int n;
int aint[dim << 1];

void Build() {

    for (int i = n - 1; i > 0; --i)
        aint[i] = max(aint[i << 1], aint[(i << 1) | 1]);

}

void Update(int pos, int value) {

    for (aint[pos += n] = value; pos > 1; pos >>= 1)
        aint[pos >> 1] = max(aint[pos], aint[pos^1]);

}

int Query(int left, int right) {

    int ret = -1;
    for (left += n, right += n; left < right; left >>= 1, right >>= 1) {

        if (left & 1) ret = max(ret, aint[left++]);
        if (right & 1) ret = max(ret, aint[--right]);

    }

    return ret;

}

int main() {

    fin >> n;
    int opCount; fin >> opCount;

    for (int i = 0; i < n; ++i)
        fin >> aint[n + i];
    Build();

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

        int op; fin >> op;
        if (op == 0) {
            int a, b;
            fin >> a >> b; --a, --b;
            fout << Query(a, b + 1) << '\n';
        }
        else {
            int a, b;
            fin >> a >> b; --a;
            Update(a, b);
        }

    }

    return 0;

}

//Trust me, I'm the Doctor!