fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String args[]) {
  5. Scanner s = new Scanner(System.in);
  6. int n = s.nextInt();
  7. long q = s.nextLong();
  8. long[] arr = new long[n + 1];
  9. for (int i = 0; i < n; i++) { //there was an error here in your previous code.
  10. arr[i] = s.nextInt();
  11. }
  12. long[] tree = new long[4 * n + 1];
  13. for(int i=0;i<tree.length;i++) //added
  14. tree[i]=-1; //added
  15.  
  16. // buildTree(arr, 1, n, tree, 1);
  17. buildTree(arr, 0, n-1, tree, 1); //there was an error here in your previous code.
  18.  
  19. while (q-- > 0) {
  20. int what = s.nextInt();
  21. if (what == 1) {
  22. int l = s.nextInt();
  23. int r = s.nextInt();
  24. long min = query(0, n-1, l-1, r-1, tree, 1);//there was an error here in your previous code.
  25. System.out.println(min);
  26. } else {
  27. int x = s.nextInt();
  28. int y = s.nextInt();
  29. updateNode(tree, 0, n-1, x-1, y, 1); //there was an error here in your previous code.
  30. }
  31. }
  32.  
  33. }
  34.  
  35. public static void buildTree(long[] arr, int s, int e, long[] tree, int index) {
  36. if (s == e) {
  37. tree[index] = arr[s];
  38. return;
  39. }
  40. int mid = (s + e) / 2;
  41. buildTree(arr, s, mid, tree, 2 * index);
  42. buildTree(arr, mid + 1, e, tree, 2 * index + 1);
  43. tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
  44. return;
  45. }
  46.  
  47. public static long query(long ss, long se, int qs, int qe, long[] tree, int index) {
  48. // complete
  49. if (ss >= qs && se <= qe) {
  50. return tree[index];
  51. }
  52. // no overlap
  53. if (ss > qe || qs > se)
  54. return Integer.MAX_VALUE;
  55. // partial
  56. long mid = (ss + se) / 2;
  57. long left = query(ss, mid, qs, qe, tree, 2 * index);
  58. long right = query(mid + 1, se, qs, qe, tree, 2 * index + 1);
  59. return Math.min(left, right);
  60. }
  61.  
  62. public static void updateNode(long[] tree, int ss, int se, int i, int increment, int index) {
  63. // no overlap
  64. if (i > se || i < ss)
  65. return;
  66. // complete
  67. if (ss == se) {
  68. tree[index] = increment; //there was an error here in your previous code.
  69. return;
  70. }
  71. // partial
  72. int mid = (ss + se) / 2;
  73. updateNode(tree, ss, mid, i, increment, 2 * index);
  74. updateNode(tree, mid + 1, se, i, increment, 2 * index + 1);
  75. tree[index] = Math.min(tree[2 * index], tree[2 * index + 1]);
  76. return;
  77. }
  78. }
  79.  
Success #stdin #stdout 0.12s 35396KB
stdin
5 5
1 4 3 5 2
1 1 5
2 3 9
1 2 4
1 2 5
1 3 4
stdout
1
4
2
5