Cod sursa(job #2765298)

Utilizator mihai_popaPopa Mihai-Razvan mihai_popa Data 26 iulie 2021 11:26:41
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5 + 5; /// 10 ^ 5

int n, m, v[NMAX], a[4 * NMAX], q, l, r, p, val;

void build (int nod, int L, int R)
{
    if(L == R)
    {
        a[nod] = v[L];

        return;
    }

    int mij = (L + R) >> 1;

    build (2 * nod, L, mij);
    build (2 * nod + 1, mij + 1, R);

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

void update (int nod, int L, int R, int val, int pos)
{
    int mij = (L + R) >> 1;

    if(L == R)
    {
        a[nod] = val;

        return;
    }

    if(pos <= mij)
        update(2 * nod, L, mij, val, pos);
    else
        update(2 * nod + 1, mij + 1, R, val, pos);

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

int querry (int nod, int L, int R, int ql, int qr)
{
    if(ql <= L && R <= qr)
        return a[nod];

    int mij = (L + R) >> 1;
    int p_Left = 0, p_Right = 0;

    if(ql <= mij)
        p_Left = querry(2 * nod, L, mij, ql, qr);

    if(qr > mij)
        p_Right = querry(2 * nod + 1, mij + 1, R, ql, qr);

    return max(p_Left, p_Right);
}

int main()
{
    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 >> q;

        if(q == 0)
        {
            fin >> l >> r;
            fout << querry(1, 1, n, l, r) << '\n';
        }
        else
        {
            fin >> p >> val;
            update(1, 1, n, val, p);
        }
    }

    return 0;
}