Cod sursa(job #1705538)

Utilizator xtreme77Patrick Sava xtreme77 Data 20 mai 2016 19:09:39
Problema Arbori de intervale Scor 50
Compilator java Status done
Runda Arhiva educationala Marime 3.37 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, IOException {
      //Reader Goo = new Reader () ;
      Reader.init(new FileInputStream ( "arbint.in" ) ); 
      PrintStream output = new PrintStream(new FileOutputStream("arbint.out"));
      int N = Reader.nextInt() ;
      int M = Reader.nextInt() ; 
      for ( int i = 1 ; i <= N ; ++ i ) {
          int X = Reader.nextInt() ; 
          update ( 1 , 1 , N , i , X ) ;
      }
      for ( int i = 1 ; i <= M ; ++ i ) {
        int Tip = Reader.nextInt() ;
        if ( Tip == 1 ) {
            int X = Reader.nextInt() ; 
            int Y = Reader.nextInt() ; 
            update ( 1 , 1 , N , X , Y ) ;
        }
        else {
            int X = Reader.nextInt() ; 
            int Y = Reader.nextInt() ; 
            output.print( query ( 1 , 1 , N , X , Y ) );
            output.print( "\n" );
        }
      }
      output.close ( ) ;
       
    }
    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 ] ) ;
    }
    /** Class for buffered reading int and double values */
    static class Reader {
        static BufferedReader reader;
        static StringTokenizer tokenizer;

        /** call this method to initialize reader for InputStream */
        static void init(InputStream input) throws IOException {
            reader = new BufferedReader(
                         new InputStreamReader(input) );
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        static String next() {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
            	try {
            		tokenizer = new StringTokenizer(reader.readLine() );
            	}catch ( Exception e ) {
            		System.out.println("ne-am luat muie");
            	}
                       
            }
            return tokenizer.nextToken();
        }

        static int nextInt() throws IOException {
            return Integer.parseInt( next() );
        }
    	
        static double nextDouble() throws IOException {
            return Double.parseDouble( next() );
        }
    }
}