Cod sursa(job #1704707)

Utilizator xtreme77Patrick Sava xtreme77 Data 19 mai 2016 11:17:23
Problema Arbori de intervale Scor 50
Compilator java Status done
Runda Arhiva educationala Marime 1.92 kb
//package arbint;

import java.util.*;
import java.io.*;

/***
* 
* @author Patrick
*
*/

public class Main {
	public static int [ ] arb = new int [ 700999 ] ;
	public static int sol ;
	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() ; 
      		sol = 0 ;
      		query ( 1 , 1 , N , X , Y ) ;
      		output.print( sol );
      		output.print( "\n" );
      	}
      		
      }
      
	}
	public static void 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 ;
		}
		int mij = ( st + dr ) / 2 ; 
		if ( a <= mij ) {
			query ( nod * 2 , st , mij , a , b ) ;
		}
		if ( b > mij ) {
			query ( nod * 2 + 1 , mij + 1 , dr , a , b ) ;
		}
		return ;
	}
	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 ] ) ;
	}
}