Cod sursa(job #3346137)

Utilizator M132M132 M132 M132 Data 12 martie 2026 17:34:20
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 = 4000002;
int arb[nmax], n, m;

int query(int a, int b, int st, int dr, int nod)
{
    if(a <= st && dr <= b)
        return arb[nod];
    int mijl = (st + dr) / 2;
    int mx = -1;
    if(a <= mijl)
        mx = max(mx, query(a, b, st, mijl, 2 * nod));
    if(b > mijl)
        mx = max(mx, query(a, b, mijl+1, dr, 2 * nod + 1));
    return mx;
}

void update(int poz, int val, int st, int dr, int nod)
{
    if(st == dr)
    {
        arb[nod] = val;
        return;
    }
    int mijl = (st + dr)/2;
    if(poz <= mijl)
        update(poz, val, st, mijl, 2*nod);
    else
        update(poz, val, mijl+1, dr, 2*nod+1);
    arb[nod] = max(arb[nod*2], arb[nod*2+1]);
}

int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        int x;
        f >> x;
        update(i, x, 1, n, 1);
    }
    while(m--)
    {
        int tip, x, y;
        f >> tip >> x >> y;
        if(tip == 0)
            g << query(x, y, 1, n, 1) << "\n";
        else
            update(x, y, 1, n, 1);
    }
    return 0;
}