Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int n = sc.nextInt();
  8. FT tree = new FT(n);
  9. tree.build();
  10. boolean t = false;
  11. while (!t) {
  12. String cmd = sc.next();
  13. switch (cmd.charAt(0)) {
  14. case '1': {
  15. int x = sc.nextInt();
  16. int y = sc.nextInt();
  17. int z = sc.nextInt();
  18. int k = sc.nextInt();
  19. tree.add(x, y, z, k);
  20. break;
  21. }
  22.  
  23.  
  24. case '2': {
  25. int x1 = sc.nextInt();
  26. int y1 = sc.nextInt();
  27. int z1 = sc.nextInt();
  28. int x2 = sc.nextInt();
  29. int y2 = sc.nextInt();
  30. int z2 = sc.nextInt();
  31. System.out.println(tree.sumR(x1, y1, z1, x2, y2, z2));
  32. break;
  33.  
  34. }
  35. case '3': {
  36. t = true;
  37. break;
  38. }
  39.  
  40. }
  41. }
  42.  
  43. }
  44.  
  45.  
  46. static class FT {
  47. int[][][] t;
  48. int n;
  49.  
  50. FT(int n) {
  51. t = new int[n][n][n];
  52. this.n = n;
  53. }
  54.  
  55. void build() {
  56. for (int i = 0; i < n; i++) {
  57. for (int j = 0; j < n; j++) {
  58. for (int k = 0; k < n; k++) {
  59. t[i][j][k] = 0;
  60. }
  61.  
  62. }
  63. }
  64. }
  65.  
  66. void add(int x, int y, int z, int c) {
  67. t[x][y][z] += c;
  68. }
  69.  
  70. int sum(int x, int y, int z) {
  71. int result = 0;
  72. for (int i = x; i >= 0; i --) {
  73. for (int j = y; j >= 0; j --) {
  74. for (int k = z; k >= 0; k --) {
  75. result += t[i][j][k];
  76. }
  77. }
  78. }
  79. return result;
  80.  
  81.  
  82. }
  83.  
  84. int sumR(int x1, int y1, int z1, int x2, int y2, int z2) {
  85. int mx = Math.min(x1, x2);
  86. int my = Math.min(y1, y2);
  87. int mz = Math.min(z1, z2);
  88. int Mx = Math.max(x1, x2);
  89. int My = Math.max(y1, y2);
  90. int Mz = Math.max(z1, z2);
  91.  
  92. return sum(Mx, My, Mz) - sum(mx, my, mz)+t[mx][my][mz];
  93.  
  94. }
  95.  
  96.  
  97. }
  98. }
  99. close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement