Cod sursa(job #3349713)

Utilizator stefan1010Stefan Bogdan stefan1010 Data 1 aprilie 2026 22:31:20
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <iostream>
const int NMAX = 1e5 + 5;
const int VMAX = 2e9;
const int VMIN = -1e9;
using namespace std;
ifstream in("arbint.in");
ofstream out("arbint.out");

int n;
int q, rez;
int A[4 * NMAX], V[NMAX];

void build(int nod, int st, int dr)
{
    if (st == dr)
        A[nod] = V[st];
    else
    {
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        A[nod] = max(A[2 * nod], A[2 * nod + 1]);
    }
}

void update(int nod, int st, int dr, int p, int x)
{
    if (st == dr)
        A[nod] = x;
    else
    {
        int mid = (st + dr) / 2;
        if (p <= mid)
            update(2 * nod, st, mid, p, x);
        else
            update(2 * nod + 1, mid + 1, dr, p, x);
        A[nod] = max(A[2 * nod], A[2 * nod + 1]);
    }
}

void query(int nod, int st, int dr, int a, int b)
{
    if (a <= st && dr <= b)
        rez = max(rez, A[nod]);
    else
    {
        int mid = (st + dr) / 2;
        if (a <= mid)
            query(2 * nod, st, mid, a, b);
        if (b >= mid + 1)
            query(2 * nod + 1, mid + 1, dr, a, b);
    }
}

int main()
{
    in >> n >> q;
    for (int i = 1; i <= n; i++)
        in >> V[i];
    build(1, 1, n);
    while (q--)
    {
        int type, a, b;
        in >> type;
        if (type == 1) /// update
        {
            in >> a >> b;
            update(1, 1, n, a, b);
        }
        else if (type == 0)
        {
            in >> a >> b;
            rez = VMIN;
            query(1, 1, n, a, b);
            out << rez << '\n';
        }
    }
    return 0;
}