Cod sursa(job #3369565)

Utilizator Seba1030Banescu Stefan Sebastian Seba1030 Data 21 octombrie 2026 09:49:10
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <bits/stdc++.h>

const int MAXN = 100000;

using namespace std;

int v[MAXN + 5], a[4 * MAXN + 5];

void build( int nod, int st, int dr ) {
    if ( st == dr ) {
        a[nod] = v[st];
        return;
    }
    int mid = ( st + dr ) / 2;
    build( 2 * nod, st, mid );
    build( 2 * nod + 1, mid + 1, dr );
    a[nod] = max( a[2 * nod], a[2 * nod + 1] );
}

int query( int nod, int st, int dr, int x, int y ) {
    if ( x <= st && dr <= y ) {
        return a[nod];
    }
    int mid = ( st + dr ) / 2;
    int ans = -1e9;
    if ( x <= mid ) {
        ans = max( ans, query( 2 * nod, st, mid, x, y ) );
    }
    if ( y > mid ) {
        ans = max( ans, query( 2 * nod + 1, mid + 1, dr, x, y ) );
    }
    return ans;
}

void update( int nod, int st, int dr, int poz, int val ){
    if ( st == dr ) {
        a[nod] = val;
        v[poz] = val;
        return;
    }
    int mid = ( st + dr ) / 2;
    if ( poz <= mid ) {
        update( 2 * nod, st, mid, poz, val );
    }
    else {
        update( 2 * nod + 1, mid + 1, dr, poz, val );
    }
    a[nod] = max( a[2 * nod], a[2 * nod + 1] );
}

int main() {
    ifstream cin( "arbint.in" );
    ofstream cout( "arbint.out" );
    int n, m;
    cin >> n >> m;
    for( int i = 1; i <= n; i++ ) {
        cin >> v[i];
    }
    build( 1, 1, n );
    for( int i = 0; i < m; i++ ) {
        int op;
        cin >> op;
        if( op == 1 ) {
            int poz, val;
            cin >> poz >> val;
            update( 1, 1, n, poz, val );
        }
        else {
            int st, dr;
            cin >> st >> dr;
            cout << query( 1, 1, n, st, dr ) << '\n';
        }
    }

    return 0;
}