/* package whatever; // don"t place package name! */
import java.util.* ;
import java.lang.* ;
import java.io.* ;
/**
* An array with special operations.
* @param <T> the type of elements in this array
*
* @author Igor Mazurok
*/
interface SegmentedArray< T> {
/**
* <p>Get value by element index</p>
* @param index index of element.
* @return Value of element with specified index.
*/
T get( int index) ;
/**
* <p>Set the same value for all elements in the specified index range</p>
* @param start the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value value for .
* @return This object.
*/
SegmentedArray< T> set( int start, int end, T value) ;
/**
* <p>Returns the index within this array of the first occurrence of the specified value,
* starting at the specified index. If no such value of k exists, then -1 is returned.</p>
* @param value the T-based value for which to search.
* @param fromIndex the index from which to start the search.
* @return the index within this array of the first occurrence of the element with specified value,
* starting at the specified index.
*/
int indexOf( T value, int fromIndex) ;
/**
* <p>Find minimum value in the specified indexes range</p>
* @param start the beginning index, inclusive.
* @param end the ending index, exclusive.
* @return Minimum value.
*/
T minValue( int from, int to) ;
}
class DummyArray implements SegmentedArray< Integer> {
return 0 ;
}
public SegmentedArray
< Integer
> set
( int start,
int end,
Integer value
) { return this ;
}
public int indexOf
( Integer value,
int fromIndex
) { return 0 ;
}
public Integer minValue
( int from,
int to
) { return 0 ;
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void testSegmentedArray1( SegmentedArray< Integer> arr) {
long start
= System .
nanoTime ( ) ;
//Предположим что параметром метода является
//нулевой массив размера 1000
if ( arr.get ( 30 ) != 0 )
System .
out .
println ( "Non zero element in empty array" ) ; if ( arr.minValue ( 0 ,1000 ) != 0 )
System .
out .
println ( "Non zero minimum in empty array" ) ; arr.set ( 0 ,200 ,1 ) ;
arr.set ( 100 ,300 ,2 ) ;
arr.set ( 150 ,160 ,1 ) ;
if ( arr.get ( 30 ) != 1 )
System .
out .
println ( "Element is not set" ) ; if ( arr.get ( 300 ) != 0 )
System .
out .
println ( "End should be exclusive" ) ; if ( arr.get ( 125 ) != 2 )
System .
out .
println ( "Element is not set (2 assigments)" ) ; if ( arr.get ( 150 ) != 1 )
System .
out .
println ( "Element is not set (3 assigments)" ) ; if ( arr.minValue ( 100 ,300 ) != 1 )
System .
out .
println ( "Wrong minimum calculation (two values)" ) ; if ( arr.minValue ( 100 ,301 ) != 0 )
System .
out .
println ( "Wrong minimum calculation (two values and zero)" ) ; if ( arr.minValue ( 155 ,156 ) != 1 )
System .
out .
println ( "Wrong minimum calculation (single element)" ) ;
long finish
= System .
nanoTime ( ) ;
System .
out .
println ( "Test 1 (small array) completed!" ) ; System .
out .
format ( "Time elapsed: %.3f ms" ,
( finish
- start
) / 1e6
) ; // ns -> ms
}
{
testSegmentedArray1( new DummyArray( ) ) ;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uInQgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgoKCi8qKgogKiBBbiBhcnJheSB3aXRoIHNwZWNpYWwgb3BlcmF0aW9ucy4gIAogKiBAcGFyYW0gPFQ+IHRoZSB0eXBlIG9mIGVsZW1lbnRzIGluIHRoaXMgYXJyYXkKICoKICogQGF1dGhvciAgSWdvciBNYXp1cm9rCiAqLwogCmludGVyZmFjZSBTZWdtZW50ZWRBcnJheTxUPiB7CiAgIC8qKgoJKiA8cD5HZXQgdmFsdWUgYnkgZWxlbWVudCBpbmRleDwvcD4KCSogQHBhcmFtIGluZGV4IGluZGV4IG9mIGVsZW1lbnQuCgkqIEByZXR1cm4gIFZhbHVlIG9mIGVsZW1lbnQgd2l0aCBzcGVjaWZpZWQgaW5kZXguCgkqLwoJVCBnZXQoaW50IGluZGV4KTsKIAogICAvKioKCSogPHA+U2V0IHRoZSBzYW1lIHZhbHVlIGZvciBhbGwgZWxlbWVudHMgaW4gdGhlIHNwZWNpZmllZCBpbmRleCByYW5nZTwvcD4KCSogQHBhcmFtIHN0YXJ0IHRoZSBiZWdpbm5pbmcgaW5kZXgsIG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