Cod sursa(job #3036567)

Utilizator Andrei_IcriverziThe Rock Andrei_Icriverzi Data 24 martie 2023 16:30:57
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in ("arbint.in");
ofstream out ("arbint.out");

int v[400001];

void update (int node, int from, int to, int pos, int val){
    if (from==to){
        v[node]=val;
        return;
    }
    int mid = (from+to)/2;
    if (pos<=mid){
        update(node*2, from,mid,pos,val);
    } else {
        update(node*2+1, mid+1,to,pos,val);
    }
    v[node]=max(v[node*2],v[node*2+1]);
}
int query (int node, int from, int to, int qleft, int qright){
    int smax=0;
    if (qleft<=from && to<=qright){
        return v[node];
    }
    int mid=(from+to)/2;
    if (qleft<=mid){
        int s=query(node*2,from,mid,qleft,qright);
        smax=max(s,smax);
    }
    if (mid+1<=qright){
        int s=query(node*2+1,mid+1,to,qleft,qright);
        smax=max(s,smax);
    }
    return smax;
}
int main()
{
    int n,m;
    
    cin >> n >> m;
    for (int i=1; i<=n; i++){
        int b;
        in >> b;
        update(1,1,n,i,b);
    }
    for (int i=1; i<=m; i++){
        int c,a,b;
        in >> c >> a >> b;
        if (c==1){
            update(1,1,n,a,b);
        } else {
           out <<  query(1,1,n,a,b) << '\n';
        }
    }
    return 0;
}