fork(1) download
  1. /* package whatever; // don"t place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7.  
  8.  
  9. /**
  10.  * An array with special operations.
  11.  * @param <T> the type of elements in this array
  12.  *
  13.  * @author Igor Mazurok
  14.  */
  15.  
  16. interface SegmentedArray<T> {
  17. /**
  18. * <p>Get value by element index</p>
  19. * @param index index of element.
  20. * @return Value of element with specified index.
  21. */
  22. T get(int index);
  23.  
  24. /**
  25. * <p>Set the same value for all elements in the specified index range</p>
  26. * @param start the beginning index, inclusive.
  27. * @param end the ending index, exclusive.
  28. * @param value value for .
  29. * @return This object.
  30. */
  31. SegmentedArray<T> set(int start, int end, T value);
  32.  
  33. /**
  34. * <p>Returns the index within this array of the first occurrence of the specified value,
  35. * starting at the specified index. If no such value of k exists, then -1 is returned.</p>
  36. * @param value the T-based value for which to search.
  37. * @param fromIndex the index from which to start the search.
  38. * @return the index within this array of the first occurrence of the element with specified value,
  39. * starting at the specified index.
  40. */
  41. int indexOf(T value, int fromIndex);
  42.  
  43. /**
  44. * <p>Find minimum value in the specified indexes range</p>
  45. * @param start the beginning index, inclusive.
  46. * @param end the ending index, exclusive.
  47. * @return Minimum value.
  48. */
  49. T minValue(int from, int to);
  50. }
  51.  
  52. class DummyArray implements SegmentedArray<Integer> {
  53. public Integer get(int index){
  54. return 0;
  55. }
  56.  
  57. public SegmentedArray<Integer> set(int start, int end, Integer value){
  58. return this;
  59. }
  60.  
  61.  
  62. public int indexOf(Integer value, int fromIndex){
  63. return 0;
  64. }
  65.  
  66.  
  67. public Integer minValue(int from, int to){
  68. return 0;
  69. }
  70.  
  71. }
  72.  
  73. /* Name of the class has to be "Main" only if the class is public. */
  74. class Ideone
  75. {
  76. public static void testSegmentedArray1(SegmentedArray<Integer> arr) {
  77. long start = System.nanoTime();
  78.  
  79. //Предположим что параметром метода является
  80. //нулевой массив размера 1000
  81. if (arr.get(30)!=0)
  82. System.out.println("Non zero element in empty array");
  83. if (arr.minValue(0,1000)!=0)
  84. System.out.println("Non zero minimum in empty array");
  85. arr.set(0,200,1);
  86. arr.set(100,300,2);
  87. arr.set(150,160,1);
  88. if (arr.get(30)!=1)
  89. System.out.println("Element is not set");
  90. if (arr.get(300)!=0)
  91. System.out.println("End should be exclusive");
  92. if (arr.get(125)!=2)
  93. System.out.println("Element is not set (2 assigments)");
  94. if (arr.get(150)!=1)
  95. System.out.println("Element is not set (3 assigments)");
  96. if (arr.minValue(100,300)!=1)
  97. System.out.println("Wrong minimum calculation (two values)");
  98. if (arr.minValue(100,301)!=0)
  99. System.out.println("Wrong minimum calculation (two values and zero)");
  100. if (arr.minValue(155,156)!=1)
  101. System.out.println("Wrong minimum calculation (single element)");
  102.  
  103. long finish = System.nanoTime();
  104.  
  105. System.out.println("Test 1 (small array) completed!");
  106. System.out.format("Time elapsed: %.3f ms", (finish-start)/1e6); // ns -> ms
  107.  
  108. }
  109.  
  110. public static void main (String[] args) throws java.lang.Exception
  111. {
  112. testSegmentedArray1(new DummyArray());
  113. }
  114. }
Success #stdin #stdout 0.13s 320576KB
stdin
Standard input is empty
stdout
Element is not set
Element is not set (2 assigments)
Element is not set (3 assigments)
Wrong minimum calculation (two values)
Wrong minimum calculation (single element)
Test 1 (small array) completed!
Time elapsed: 0.683 ms