Cod sursa(job #3316246)

Utilizator petric_mariaPetric Maria petric_maria Data 18 octombrie 2025 00:23:34
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");

int n, m, a[300005], p, t, x, y;

int maxim (int stq, int drq, int stn, int drn, int indn) {
    if (stq <= stn && drq >= drn)
        return a[indn];
    int ma = 0;
    if (stq <= (stn + drn)/2)
        ma = maxim (stq, drq, stn, (stn+drn)/2, indn*2);
    if (drq > (stn + drn)/2)
        ma = max(ma, maxim (stq, drq, (stn+drn)/2 + 1, drn, indn*2+1));
    return ma;
}

void update (int poz, int val) {
    a[poz] = val;
    while (poz >= 1) {
        poz /= 2;
        a[poz] = max (a[poz*2], a[poz*2+1]);
    }
}

int main()
{
    f >> n >> m;
    p = 1;
    while (p < n)
        p *= 2;
    for (int i=1; i<=n; ++i)
        f >> a[p+i-1];
    for (int i=p-1; i>=1; --i)
        a[i] = max (a[i*2], a[i*2+1]);
    for (int i=1; i<=m; ++i) {
        f >> t >> x >> y;
        if (t == 0)
            g << maxim (x, y, 1, p, 1) << '\n';
        else
            update (x+p-1, y);
    }
    return 0;
}