Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- FT tree = new FT(n);
- tree.build();
- boolean t = false;
- while (!t) {
- String cmd = sc.next();
- switch (cmd.charAt(0)) {
- case '1': {
- int x = sc.nextInt();
- int y = sc.nextInt();
- int z = sc.nextInt();
- int k = sc.nextInt();
- tree.add(x, y, z, k);
- break;
- }
- case '2': {
- int x1 = sc.nextInt();
- int y1 = sc.nextInt();
- int z1 = sc.nextInt();
- int x2 = sc.nextInt();
- int y2 = sc.nextInt();
- int z2 = sc.nextInt();
- System.out.println(tree.sumR(x1, y1, z1, x2, y2, z2));
- break;
- }
- case '3': {
- t = true;
- break;
- }
- }
- }
- }
- static class FT {
- int[][][] t;
- int n;
- FT(int n) {
- t = new int[n][n][n];
- this.n = n;
- }
- void build() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- for (int k = 0; k < n; k++) {
- t[i][j][k] = 0;
- }
- }
- }
- }
- void add(int x, int y, int z, int c) {
- t[x][y][z] += c;
- }
- int sum(int x, int y, int z) {
- int result = 0;
- for (int i = x; i >= 0; i --) {
- for (int j = y; j >= 0; j --) {
- for (int k = z; k >= 0; k --) {
- result += t[i][j][k];
- }
- }
- }
- return result;
- }
- int sumR(int x1, int y1, int z1, int x2, int y2, int z2) {
- int mx = Math.min(x1, x2);
- int my = Math.min(y1, y2);
- int mz = Math.min(z1, z2);
- int Mx = Math.max(x1, x2);
- int My = Math.max(y1, y2);
- int Mz = Math.max(z1, z2);
- return sum(Mx, My, Mz) - sum(mx, my, mz)+t[mx][my][mz];
- }
- }
- }
- close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement