Cod sursa(job #2128594)

Utilizator Alexandru_StoianStoian Sorin Alexandru Alexandru_Stoian Data 11 februarie 2018 20:46:24
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <climits>

#define dim 100001

using namespace std;

ifstream f ("arbint.in");
ofstream g ("arbint.out");

inline void Update( int nod, int st, int dr );

inline void Querry( int nod, int st, int dr );

int n, m, Arbmax[ 4 * dim + 10 ], nod, st, dr, val, poz, ok, a, b, sf, in, maxim;

int main(){
    f >> n >> m;
    for( int i = 1; i <= n; ++i ){
        f >> val;
        poz = i;
        Update( 1, 1, n );
    }
    for( int i = 1; i <= m; ++i ){
        f >> ok >> a >> b;
        if( ok == 0 ){
            maxim = -1;
            in = a, sf = b;
            Querry( 1, 1, n );
            g << maxim << '\n';
        }
        else{
            val = b;
            poz = a;
            Update( 1, 1, n );
        }
    }
    return 0;
}

inline void Update( int nod, int st, int dr ){
    if( st == dr ){
        Arbmax[ nod ] = val;
        return;
    }
    int div = ( st + dr ) / 2;
    if( poz <= div )Update( nod * 2, st, div );
    else Update( nod * 2 + 1, div + 1, dr );
    Arbmax[ nod ] = max( Arbmax[ 2 * nod ], Arbmax[ 2 * nod + 1 ] );
}

inline void Querry( int nod, int st, int dr ){
    if( in <= st && dr <= sf ){
        cout << '\n';
        if( maxim < Arbmax[ nod ] )maxim = Arbmax[ nod ];
        return;
    }
    int div = ( st + dr ) / 2;
    if( in <= div )Querry( nod * 2, st, div );
    if( div < sf )Querry( nod * 2 + 1, div + 1, dr );
}