Cod sursa(job #2201168)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 3 mai 2018 20:01:20
Problema Arbori de intervale Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, q, a[100006], aint[100006];
void Citire()
{
    fin >> n >> q;
    for(int i = 1; i <= n; i++) fin >> a[i];
}

void Build(int nod, int x, int y)
{
    if(x == y)
    {
        aint[nod] = a[x];
        return;
    }

    int mij = (x + y) / 2;
    Build(2 * nod, x, mij); ///Parcurgere subarbore stang
    Build(2 * nod + 1, mij + 1, y); ///Parcurgere subarbore drept

    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    ///Actualizare valoare maxima
}

void U(int nod, int x, int y, int D, int V)
{
    if(x == y)
    {
        aint[nod] = V;
        return;
    }

    int mij = (x + y) / 2;

    if(D <= mij)
        U(2 * nod, x, mij, D, V);
    else U(2 * nod + 1, mij + 1, y, D, V);

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

int Q(int nod, int x, int y, int a, int b)
{
    if(x == a && y == b)
        return aint[nod];

    int mij = (x + y) / 2;
    if(b <= mij)
        return Q(2 * nod, x, mij, a, b);
    else if(a > mij)
        return Q(2 * nod + 1, mij + 1, y, a, b);
    else return max(Q(2 * nod, x, mij, a, mij), Q(2 * nod + 1, mij + 1, y, mij + 1, b));
}

int main()
{
    int op, a, b;
    Citire();
    Build(1, 1, n);
    while(q--)
    {
        fin >> op >> a >> b;
        if(op == 1)
            U(1, 1, n, a, b);
        else fout << Q(1, 1, n, a, b) << "\n";
    }
    return 0;
}