Cod sursa(job #3312878)

Utilizator ilincaSSirbu Ilinca-Maria eu ilincaS Data 30 septembrie 2025 16:11:05
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <math.h>

using namespace std;

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

int v[262200];
int n, m, t, a, b, pt=1, tot;

inline void update(int poz, int val)
{
    poz=tot+poz-1;
    v[poz]=val;
    poz/=2;
    while(poz>0)
    {
        v[poz]=max(v[2*poz], v[2*poz+1]);
        poz/=2;
    }
}

int query(int ind, int st, int dr, int qst, int qdr)
{
    if(qst<=st && dr<=qdr)
    {
        return v[ind];
    }
    else
    {
        int mij = (st+dr)/2, mx=0;
        if(qst<=mij)
        {
            mx=max(mx, query(2*ind, st, mij, qst, qdr));
        }
        if(mij<qdr)
        {
            mx=max(mx, query(2*ind+1, mij+1, dr, qst, qdr));
        }
        return mx;
    }
}

int main()
{
    cin>>n>>m;
    tot=log2(n)+1;
    tot= (1<<tot);
    for(int i=tot; i<tot+n; i++)
    {
        cin>>v[i];
    }
    for(int i=tot-1; i>=1; i--)
    {
        v[i]=max(v[i*2], v[2*i+1]);
    }
    for(int i=0; i<m; i++)
    {
        cin>>t>>a>>b;
        if(t==0)
        {
            cout<<query(1, 1, tot, a, b)<<'\n';
        }
        else
        {
            update(a, b);
        }
    }


    return 0;
}