Cod sursa(job #1349886)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 20 februarie 2015 15:43:51
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>
#include <algorithm>

using namespace std;

#define MAX_N 100000

#define IN_FILE "arbint.in"
#define OUT_FILE "arbint.out"

int N, M;
int aint[ MAX_N << 2 ];
int ans, poz, val;
void update( const int &nod, const int &st, const int &dr ) {
    if( st == dr ) {
        aint[ nod ] = val;
        return ;
    }
    int mid = st + ( ( dr - st ) >> 1 );
    if( poz <= mid )
        update( nod << 1, st, mid );
    else
        update( ( nod << 1 ) + 1, mid + 1, dr );
    aint[ nod ] = max( aint[ nod << 1 ], aint[ ( nod << 1 ) + 1 ] );
}
void query( const int &nod, const int &st, const int &dr ) {
    if( poz <= st && val >= dr ) {
        if( aint[ nod ] > ans )
            ans = aint[ nod ];
        return;
    }
    int mid = st + ( ( dr - st ) >> 1 );
    if( poz <= mid )
        query( nod << 1, st, mid );
    if( val > mid )
        query( ( nod << 1 ) + 1, mid + 1, dr );
}
int main( ) {
    fstream f, g;
    int op, i, x, y;

    f.open( IN_FILE, ios :: in );
    f >> N >> M;
    for( i = 0; i != N; ++i ) {
        f >> x;
        poz = i + 1;
        val = x;
        update( 1, 1, N );
    }

    g.open( OUT_FILE, ios :: out );
    for( i = 0; i != M; ++i ) {
        f >> op >> x >> y;
        poz = x;
        val = y;
        if( !op ) {
            ans = 0;
            query( 1, 1, N );
            g << ans << '\n';
        } else
            update( 1, 1, N );
    }
    f.close( );
    g.close( );
    return 0;
}