Cod sursa(job #3297826)

Utilizator SimifilLavrente Simion Simifil Data 23 mai 2025 21:13:04
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("arbint.in");
ofstream g("arbint.out");

const int maxn = 100001;
int aint[4*maxn], v[maxn];

void buildaint( int index, int st, int dr )
{
    if( st == dr )
        aint[index] = v[st];
    else
    {
        int mij = st + (dr-st)/2;
        buildaint(index*2, st, mij );
        buildaint(index*2+1, mij+1, dr );
        aint[index] = max( aint[index*2], aint[index*2+1] );
    }
}

void change( int index, int st, int dr, int poz )
{
    if( st == dr && st == poz )
        aint[index] = v[st];
    else
    {
        int mij = st + (dr-st)/2;
        if( poz <= mij )
            change( index*2, st, mij, poz );
        else
            change( index*2+1, mij+1, dr, poz );
        aint[index] = max( aint[index*2], aint[index*2+1] );
    }
}

int query( int index, int st, int dr, int ST, int DR )
{
    if( ST <= st && dr <= DR )
        return aint[index];
    else
    {
        int mij = st + (dr-st)/2;
        int ans = 0;
        if( ST <= mij )
        {
            ans = max( ans, query( index*2, st, mij, ST, DR ) );
        }
        if( mij+1 <= DR )
        {
            ans = max( ans, query( index*2+1, mij+1, dr, ST, DR) );
        }
        return ans;
    }
}

int main() {
	int n, m;
    f >> n >> m;
    for( int i = 1; i <= n; ++i )
        f >> v[i];
    buildaint(1, 1, n);
    for( int i = 1; i <= m; ++i )
    {
        int op, a, b;
        f >> op >> a >> b;
        if( op == 1 )
        {
            v[a] = b;
            change( 1, 1, n, a );
        }
        else
        {
            g << query( 1, 1, n, a, b ) << "\n";
        }
    }
    return 0;
}