fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. import static java.lang.Integer.MAX_VALUE;
  7. import static java.lang.Integer.parseInt;
  8. import static java.lang.Math.min;
  9.  
  10. class SegmentTree {
  11.  
  12. static int t[] = new int[200005];
  13. static int a[] = new int[200005];
  14.  
  15. public static void main(String[] args) throws IOException {
  16. int n, l, r, q, x, y;
  17. String s[];
  18.  
  19. s = br.readLine().split("\\s");
  20. n = parseInt(s[0]);
  21. q = parseInt(s[1]);
  22.  
  23. s = br.readLine().split("\\s");
  24. for (int i = 0; i < n; i++) {
  25. a[i] = parseInt(s[i]);
  26. }
  27.  
  28. buildTree(a, n);
  29. for (int i = 0; i < q; i++) {
  30. s = br.readLine().split("\\s");
  31. if ("q".equals(s[0])) {
  32. l = parseInt(s[1]);
  33. r = parseInt(s[2]);
  34. System.out.println(query(--l, r, n));
  35. } else {
  36. x = parseInt(s[1]);
  37. y = parseInt(s[2]);
  38. update(--x, y, n);
  39. }
  40. }
  41. }
  42.  
  43. private static void update(int x, int y, int n) {
  44.  
  45.  
  46. x += n;
  47. t[x] = y;
  48. for ( ; x > 1; x >>= 1) {
  49. t[x>>1] = min(t[x], t[(x) ^ 1]);
  50. }
  51. }
  52.  
  53. private static int query(int l, int r,int n) {
  54.  
  55. int ans = Integer.MAX_VALUE;
  56. for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
  57. if ((l & 1) == 1) {
  58. ans = min(ans, t[l++]);
  59. }
  60. if ((r & 1) == 1) {
  61. ans = min(ans, t[--r]);
  62. }
  63. }
  64. return ans;
  65. }
  66.  
  67. private static void buildTree(int[] a, int n) {
  68. Arrays.fill(t, 0, 2 * n , Integer.MAX_VALUE);
  69. System.arraycopy(a, 0, t, n, n);
  70. //for (int i = 0; i < n; ++i) t[n+i]=a[i];
  71. for (int i = n - 1; i > 0; i--) {
  72. t[i] = min(t[i << 1], t[(i << 1) + 1]);
  73. }
  74. }
  75. }
Success #stdin #stdout 0.1s 29512KB
stdin
5 5
1 5 2 4 3
q 1 5
q 1 3
q 3 5
u 3 6
q 1 5
stdout
1
1
2
1