Cod sursa(job #3125719)

Utilizator usureluflorianUsurelu Florian-Robert usureluflorian Data 4 mai 2023 10:57:54
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("arbint.in");
ofstream g ("arbint.out");

const int nmax = 4e5 + 3;
int x, n, t, v[nmax], sol, caz, y;

void update(int st, int dr, int nod, int poz, int val)
{
    if (st == dr)
    {
        v[nod] = val;
        return;
    }

    int mij = (st + dr) / 2;

    if (poz <= mij) update(st, mij, 2 * nod, poz, val);
    else update(mij + 1, dr, 2 * nod + 1, poz, val);

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

void query(int st, int dr, int nod, int a, int b)
{
    if (a <= st && dr <= b)
    {
        sol = max(sol, v[nod]);
        return;
    }

    int mij = (st + dr) / 2;
    if (a <= mij) query(st, mij, 2 * nod, a, b);
    if (b > mij) query(mij + 1, dr, 2 * nod + 1, a, b);
}

int main()
{
    f >> n >> t;

    for (int i = 1; i <= n; ++i)
    {
        f >> x;
        update(1, n, 1, i, x);
    }

    while (t--)
    {
        f >> caz >> x >> y;

        if (caz)
            update(1, n, 1, x, y);

        else
        {
            sol = 0;
            query(1, n, 1, x, y);
            g << sol << '\n';
        }
    }
    return 0;
}