Cod sursa(job #3308969)

Utilizator Grama2008Grama Andrei Teodor Grama2008 Data 30 august 2025 14:55:31
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>

using namespace std;

int arb[400005],n,m;

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

int query(int l, int r, int node, int st, int dr){
    if (l<=st && dr<=r){
        return arb[node];
    }
    int mid=(st+dr)/2;
    int x=0,y=0;
    if (l<=mid){
        x=query(l,r,2*node, st, mid);
    }
    if (r>=mid+1){
        y=query(l,r,2*node+1, mid+1, dr);
    }
    return max(x,y);
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        int x;cin>>x;
        update(i,x,1,1,n);
    }
    while (m--){
        int q,a,b;cin>>q>>a>>b;
        if (q==0){
            cout<<query(a,b,1,1,n)<<'\n';
        }
        else{
            update(a,b,1,1,n);
        }
    }
    return 0;
}