Cod sursa(job #2909202)

Utilizator andreimocianAndrei Mocian andreimocian Data 9 iunie 2022 21:02:04
Problema Arbori de intervale Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n, m;
int v[100005], aint[100005];

void Build(int nod, int st, int dr)
{
    if (st == dr)
    {
        aint[nod] = v[st];
        return;
    }
    int mij = (st + dr) / 2;
    Build(2 * nod, st, mij);
    Build(2 * nod + 1, mij + 1, dr);
    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}

void Update(int nod, int st, int dr, int a, int b)
{
    if (st == dr)
    {
        aint[nod] = b;
        return;
    }
    int mij = (st + dr) / 2;
    if (a <= mij)
        Update(2 * nod, st, mij, a, b);
    else
        Update(2 * nod + 1, mij + 1, dr, a, b);
    aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
}

int Query(int nod, int st, int dr, int x, int y)
{
    if (st == x && dr == y)
    {
        return aint[nod];
    }
    int mij = (st + dr) / 2;
    if (y <= mij)
        return Query(2 * nod, st, mij, x, y);
    else if (x > mij)
        return Query(2 * nod + 1, mij + 1, dr, x, y);
    else
        return max(Query(2 * nod, st, mij, x, mij), Query(2 * nod + 1, mij + 1, dr, mij + 1, y));
}

void citire()
{
    int x, a, b;
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        fin >> v[i];
    }
    Build(1, 1, n);
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> a >> b;
        if (x == 0)
        {
            fout << Query(1, 1, n, a, b) << "\n";
        }
        else
        {
            Update(1, 1, n, a, b);
        }
    }
}

int main()
{
    citire();
    return 0;
}