Cod sursa(job #1705518)

Utilizator xtreme77Patrick Sava xtreme77 Data 20 mai 2016 18:46:25
Problema Arbori de intervale Scor 50
Compilator java Status done
Runda Arhiva educationala Marime 2.21 kb
//package arbint;
 
import java.util.*;
import java.io.*;
 
/***
* 
* @author Patrick
*
*/
 
public class Main {
    public static int [ ] arb = new int [ 700999 ] ;
    public static void main(String[] args) throws FileNotFoundException {
      Scanner scanner = new Scanner(new BufferedInputStream(new FileInputStream("arbint.in")));
      PrintStream output = new PrintStream(new FileOutputStream("arbint.out"));
      int N = scanner.nextInt() ;
      int M = scanner.nextInt() ; 
      for ( int i = 1 ; i <= N ; ++ i ) {
          int X = scanner.nextInt() ; 
          update ( 1 , 1 , N , i , X ) ;
      }
      for ( int i = 1 ; i <= M ; ++ i ) {
        int Tip = scanner.nextInt() ;
        if ( Tip == 1 ) {
            int X = scanner.nextInt() ; 
            int Y = scanner.nextInt() ; 
            update ( 1 , 1 , N , X , Y ) ;
        }
        else {
            int X = scanner.nextInt() ; 
            int Y = scanner.nextInt() ; 
            output.print( query ( 1 , 1 , N , X , Y ) );
            output.print( "\n" );
        }
             
      }
       
    }
    public static int query ( int nod , int st , int dr , int a , int b )
    {
        //System.out.println( "a intrat" );
        //System.out.println( nod );
        if ( a <= st && dr <= b ) {
            //sol = Math.max( sol , arb [ nod ] ) ;
            return arb [ nod ] ;
        }
        int mij = ( st + dr ) / 2 ; 
        int LEFT = 0 ; 
        int RIGHT = 0 ;
        if ( a <= mij ) {
            LEFT = query ( nod * 2 , st , mij , a , b ) ;
        }
        if ( b > mij ) {
            RIGHT = query ( nod * 2 + 1 , mij + 1 , dr , a , b ) ;
        }
        return Math.max( LEFT ,  RIGHT ) ;
    }
    public static void update ( int nod , int st , int dr , int pos , int val )
    {
        if ( st == dr ) {
            arb [ nod ] = val ;
            return ; 
        }
        int mij = ( st + dr ) / 2 ;
        if ( pos <= mij ) {
            update ( nod * 2 , st , mij , pos , val ) ;
        }
        else {
            update ( nod * 2 + 1 , mij + 1 , dr , pos , val ) ;
        }
        arb [ nod ] = Math.max ( arb [ nod * 2 ] , arb [ nod * 2 + 1 ] ) ;
    }
}