Cod sursa(job #1699242)

Utilizator xtreme77Patrick Sava xtreme77 Data 6 mai 2016 19:45:45
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.67 kb
/**
 * Code by Patrick Sava
 * "Spiru Haret" National College of Bucharest
 **/

# include "fstream"
# include "cstring"
# include "vector"
# include "queue"
# include "bitset"
# include "algorithm"
# include "map"
# include "set"
# include "unordered_map"
# include "deque"
# include "string"
# include "iomanip"
# include "cmath"
# include "stack"
# include "cassert"

const char IN [ ] =  "arbint.in" ;
const char OUT [ ] = "arbint.out" ;

using namespace std ;

# define pb push_back
# define mp make_pair
# define FORN( a , b , c ) for ( register int a = b ; a <= c ; ++ a )
# define FORNBACK( a , b , c ) for ( register int a = b ; a >= c ; -- a )

ifstream cin ( IN ) ;
ofstream cout ( OUT ) ;

const int MAX = 1e5 + 14 ;

class SegmentTree {

public :
    int Tree [ MAX << 1 ] ;
    int Lazy [ MAX << 1 ] ;
    void Reset ( ) {
        memset ( Tree , 0 , sizeof ( Tree ) ) ;
        memset ( Lazy , 0 , sizeof ( Lazy ) ) ;
    }
    void ElementUpdate ( int nod , int st , int dr , int pos , int val )
    {
        if ( st == dr ) {
            Tree [ nod ] = val ;
            return ;
        }
        int mij = ( st + dr ) >> 1 ;
        if ( pos <= mij ) {
            ElementUpdate ( nod << 1 , st , mij , pos , val ) ;
        }
        else {
            ElementUpdate ( nod << 1 | 1 , mij + 1 , dr , pos , val ) ;
        }
        Tree [ nod ] = max ( Tree [ nod << 1 ] , Tree [ nod << 1 | 1 ] ) ;
    }
    int RangeQuery ( int nod , int st , int dr , int a , int b )
    {
        if ( a <= st and dr <= b ) {
            return Tree [ nod ] ;
        }
        int mij = ( st + dr ) >> 1 ;
        int LeftMaximum = 0 ;
        int RightMaximum = 0 ;
        if ( a <= mij ) {
            LeftMaximum = RangeQuery ( nod << 1 , st , mij , a , b ) ;
        }
        if ( b > mij ) {
            RightMaximum = RangeQuery ( nod << 1 | 1 , mij + 1 , dr , a , b ) ;
        }
        return max ( LeftMaximum , RightMaximum ) ;
    }

};

int main ( void )
{
    int n , m ;
    cin >> n >> m ;
    SegmentTree Arbore ;
    Arbore.Reset() ;
    FORN ( i , 1 , n ) {
        int Value ;
        cin >> Value ;
        Arbore.ElementUpdate ( 1 , 1 , n , i , Value ) ;
    }
    while ( m -- ) {
        int Type ;
        cin >> Type ;
        if ( not Type ) {
            int _Left , _Right ;
            cin >> _Left >> _Right ;
            cout << Arbore.RangeQuery ( 1 , 1 , n , _Left , _Right ) << '\n' ;
        }
        else {
            int Position , Value ;
            cin >> Position >> Value ;
            Arbore.ElementUpdate ( 1 , 1 , n , Position , Value ) ;
        }
    }
    return 0;
}