Cod sursa(job #1954136)

Utilizator dumbraveanbDumbravean Bogdan dumbraveanb Data 5 aprilie 2017 10:56:36
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>

using namespace std;

#define Nmax 100005

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

int n, m, A[5 * Nmax];

void Update(const int& nod, const int& st, const int& dr, const int& val, const int& pos) {
    if(st == dr) {
        A[nod] = val;
        return;
    }
    int mij = (st + dr) / 2;
    if(pos <= mij)
        Update(2 * nod, st, mij, val, pos);
    else
        Update(2 * nod + 1, mij + 1, dr, val, pos);
}

int Query(const int& nod, const int& st, const int& dr, const int& a, const int&b) {
    if(a <= st && dr <= b)
        return A[nod];
    int mij = (st + dr) / 2;
    return max(Query(2 * nod, st, mij, a, b), Query(2 * nod + 1, mij + 1, dr, a, b));
}

int main()
{
    fin >> n >> m;
    int op, x, y;
    for(int i = 1; i <= n; ++i) {
        fin >> x;
        Update(1, 1, n, x, i);
    }
    for(int j = 1; j <= m; ++j) {
        fin >> op >> x >> y;
        if(!op)
            fout << Query(1, 1, n, x, y);
        else
            Update(1, 1, n, y, x);
    }
    return 0;
}