fork download
  1. class SegmentTreeSimple {
  2.  
  3. public static int get(int[] t, int i) {
  4. return t[i + t.length / 2];
  5. }
  6.  
  7. public static void add(int[] t, int i, int value) {
  8. i += t.length / 2;
  9. t[i] += value;
  10. for (; i > 1; i >>= 1)
  11. t[i >> 1] = Math.max(t[i], t[i ^ 1]);
  12. }
  13.  
  14. // max[a, b]
  15. public static int max(int[] t, int a, int b) {
  16. int res = Integer.MIN_VALUE;
  17. for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) {
  18. if ((a & 1) != 0)
  19. res = Math.max(res, t[a]);
  20. if ((b & 1) == 0)
  21. res = Math.max(res, t[b]);
  22. }
  23. return res;
  24. }
  25.  
  26. // Usage example
  27. public static void main(String[] args) {
  28. int n = 10;
  29. int[] t = new int[n + n];
  30. add(t, 0, 1);
  31. add(t, 9, 2);
  32. System.out.println(2 == max(t, 0, 9));
  33. }
  34. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
true