Cod sursa(job #3324966)

Utilizator Victor5539Tanase Victor Victor5539 Data 24 noiembrie 2025 12:44:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");

const int NMAX=1e5;
int n,i,A[4*NMAX+5],sol,st,dr,type,p,x,v[NMAX+5],q;


void build(int nod ,int st ,int dr)
{
    if (st==dr)
    {
        A[nod]=v[st];
    }
    else
    {
        int mij=(st+dr)>>1;

        build(2*nod,st,mij);
        build(2*nod+1,mij+1,dr);

        A[nod]=max(A[2*nod],A[2*nod+1]);
    }
}

void update(int nod ,int st ,int dr ,int p, int x)
{
    if (st==dr)
        A[nod]=x;
    else
    {
        int mij=(st+dr)>>1;

        if (p<=mij)
            update(2*nod,st,mij,p,x);
        else
            update(2*nod+1,mij+1,dr,p,x);

        A[nod]=max(A[2*nod],A[2*nod+1]);
    }
}

void query(int nod, int st , int dr , int a , int b)
{
    if (a<=st && dr<=b)
        sol=max(sol,A[nod]);
    else
    {
        int mij=(st+dr)>>1;

        if (a<=mij)
            query(2*nod,st,mij,a,b);

        if (b>mij)
            query(2*nod+1,mij+1,dr,a,b);
    }
}

int main()
{
    fin>>n>>q;

    for (i=1; i<=n; i++)
        fin>>v[i];

    build(1,1,n);

    while (q--)
    {
        fin>>type;

        if (type==0)
        {
            fin>>st>>dr;
            sol=0;

            query(1,1,n,st,dr);

            fout<<sol<<"\n";
        }
        else
        {
            fin>>p>>x;
            update(1,1,n,p,x);
        }
    }



    return 0;
}