Cod sursa(job #3142048)

Utilizator xDemonstyMatei Haba Ionut xDemonsty Data 18 iulie 2023 16:36:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.89 kb
#include <fstream>

using namespace std;

ifstream cin("arbint.in");
ofstream cout("arbint.out");
long long n, m ;
long long v [ 200005 ];

long long arbore [ 450000 ];


void build(int nod, int st, int dr )
{

    if ( st == dr )
    {
        arbore [ nod  ] = v [ st ] ;
    }
    else
    {
        int mid = ( st + dr ) / 2 ;

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

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

}

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

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

int query( int nod, int st, int dr, int left, int right  )
{
    if ( st >= left && dr <= right )
        return arbore [ nod ];
    else
    {
        int mid = ( st + dr) / 2 ;

        if ( right <= mid )
            return query ( nod * 2, st, mid, left, right );

        if ( mid + 1 <= left )
        {
            return query( nod  * 2 + 1, mid + 1, dr, left, right );
        }

        return max ( query ( nod * 2, st, mid, left, right ), query ( nod * 2 + 1, mid + 1, dr, left, right )) ;

    }


}
int main()
{
    int q ;
    cin >> n >> q ;

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

    build (  1, 1, n );
    for ( int f = 1; f <= q ; f ++ )
    {
        int tip, a, b ;
        cin >>tip>> a >> b ;

        if ( tip == 1 )
        {
            update ( 1, 1, n, a, b) ;

        }
        else
        {
            cout << query ( 1, 1, n, a, b ) << '\n';

        }
    }
    return 0;
}