Cod sursa(job #3272220)

Utilizator pacelaaaCiurea Pavel pacelaaa Data 28 ianuarie 2025 22:17:11
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>

using namespace std;

int pow2 ( int n ) {
    int i = 1;
    while ( n > 0 ) {
        i = i * 2;
        n /= 2;
    }
    return i;
}
int max ( int a, int b ) {
    if ( a > b )
      return a;
    return b;
}

int tree[ 400000 ];

int main()
{
    int m, n, i, operatie, a , b, q, x, ans;
    ifstream cin ( "arbint.in" );
    ofstream cout ( "arbint.out" );
    cin >> m >> q;
    n = pow2( m );
    for (i = 0; i < m; i ++ ) {
        cin >> tree[ i + n ];
    }
    for ( i = n - 1 ; i >= 1; i -- )
      tree[ i ] = max( tree[ 2 * i ], tree[ 2 * i + 1] );

    for ( i = 0; i < q; i ++ ) {
        cin >> operatie >> a >> b;
        if ( operatie == 1 ) {
           x = a + n - 1;
           tree[ x ] = b;
           while ( x > 1 ) {
               x /= 2;
               tree[ x ] = max( tree[2*x], tree[2*x + 1]);
           }
        }
        if ( operatie == 0 ) {
            ans = 0;
            a = a + n - 1;
            b = b + n - 1;
            while ( a <= b ) {
                if ( a % 2 == 1 )
                  ans = max( tree[a], ans );
                if ( b % 2 == 0 )
                  ans = max ( tree[b], ans );
                a = ( a + 1 )/2;
                b = ( b - 1 ) / 2;
            }
            cout << ans << "\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}