Submission #56570024


Source Code Expand

import java.io.*;
import java.math.*;
import java.util.*;

class Main {
    private static class FastInput {
        private static BufferedReader br;
        private static StringTokenizer st;
        private static String token;

        public FastInput(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
            readLine();
        }

        public FastInput(String fileName) {
            try {
                InputStream in = new FileInputStream(fileName);
                br = new BufferedReader(new InputStreamReader(in));
                readLine();
            } catch (Exception e) {}
        }

        private void readLine() {
            try {
                String line = br.readLine();
                if (line == null) {
                    st = null;
                }
                else {
                    st = new StringTokenizer(line);
                }
                token = null;
            } catch (Exception e) {}
        }

        private String peekToken() {
            if (token == null) {
                while (true) {
                    if (st == null) {
                        return null;
                    }
                    else if (st.hasMoreTokens()) {
                        token = st.nextToken();
                        break;
                    }
                    else {
                        readLine();
                    }
                }
            }
            return token;
        }

        private String nextToken() {
            String res = peekToken();
            token = null;
            return res;
        }

        public boolean hasNext() {
            return (peekToken() != null);
        }

        public boolean hasNextBigDecimal() {
            try {
                BigDecimal x = new BigDecimal(peekToken());
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextBigInteger() {
            try {
                BigInteger x = new BigInteger(peekToken());
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextBoolean() {
            try {
                boolean x = Boolean.parseBoolean(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextByte() {
            try {
                byte x = Byte.parseByte(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextDouble() {
            try {
                double x = Double.parseDouble(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextFloat() {
            try {
                float x = Float.parseFloat(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextInt() {
            try {
                int x = Integer.parseInt(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextLine() {
            return (st != null);
        }

        public boolean hasNextLong() {
            try {
                long x = Long.parseLong(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public boolean hasNextShort() {
            try {
                short x = Short.parseShort(peekToken());
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }

        public String next() {
            return nextToken();
        }

        public BigDecimal nextBigDecimal() {
            BigDecimal x = new BigDecimal(nextToken());
            return x;
        }

        public BigInteger nextBigInteger() {
            BigInteger x = new BigInteger(nextToken());
            return x;
        }

        public boolean nextBoolean() {
            boolean x = Boolean.parseBoolean(nextToken());
            return x;
        }

        public byte nextByte() {
            byte x = Byte.parseByte(nextToken());
            return x;
        }

        public double nextDouble() {
            double x = Double.parseDouble(nextToken());
            return x;
        }

        public float nextFloat() {
            float x = Float.parseFloat(nextToken());
            return x;
        }

        public int nextInt() {
            int x = Integer.parseInt(nextToken());
            return x;
        }

        public String nextLine() {
            String res = (token == null ? "" : token);
            try {
                res += st.nextToken("");
            }
            catch (Exception e) {}
            readLine();
            return res;
        }

        public long nextLong() {
            long x = Long.parseLong(nextToken());
            return x;
        }

        public short nextShort() {
            short x = Short.parseShort(nextToken());
            return x;
        }
    }

    private static class FastOutput {
        private static BufferedWriter bw;

        public FastOutput(OutputStream out) {
            bw = new BufferedWriter(new OutputStreamWriter(out));
        }

        public FastOutput(String fileName) {
            try {
                OutputStream out = new FileOutputStream(fileName);
                bw = new BufferedWriter(new OutputStreamWriter(out));
            } catch (Exception e) {}
        }

        public void flush() {
            try {
                bw.flush();
            } catch (Exception e) {}
        }

        public void print(boolean b) {
            print(String.valueOf(b));
        }

        public void print(char c) {
            print(String.valueOf(c));
        }

        public void print(char[] s) {
            try {
                bw.write(s, 0, s.length);
            } catch (Exception e) {}
        }

        public void print(double d) {
            print(String.valueOf(d));
        }

        public void print(float f) {
            print(String.valueOf(f));
        }

        public void print(int i) {
            print(String.valueOf(i));
        }

        public void print(long l) {
            print(String.valueOf(l));
        }

        public void print(Object obj) {
            print(String.valueOf(obj));
        }

        public void print(String s) {
            try {
                bw.write(s, 0, s.length());
            } catch (Exception e) {}
        }

        public void println() {
            try {
                bw.newLine();
            } catch (Exception e) {}
        }

        public void println(boolean x) {
            print(x);
            println();
        }

        public void println(char x) {
            print(x);
            println();
        }

        public void println(char[] x) {
            print(x);
            println();
        }

        public void println(double x) {
            print(x);
            println();
        }

        public void println(float x) {
            print(x);
            println();
        }

        public void println(int x) {
            print(x);
            println();
        }

        public void println(long x) {
            print(x);
            println();
        }

        public void println(Object x) {
            print(x);
            println();
        }

        public void println(String x) {
            print(x);
            println();
        }
    }

    private static final FastInput in = new FastInput(System.in);
    private static final FastOutput out = new FastOutput(System.out);

    public static void main(String[] args) {
        justDoIt();
        out.flush();
    }

    private static void justDoIt() {
        int n = in.nextInt();
        long[][][] pref = new long[n + 1][n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    pref[i][j][k] = in.nextInt();
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    pref[i][j][k] += pref[i][j][k - 1];
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    pref[i][j][k] += pref[i][j - 1][k];
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= n; k++) {
                    pref[i][j][k] += pref[i - 1][j][k];
                }
            }
        }
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            int lx = in.nextInt();
            int rx = in.nextInt();
            int ly = in.nextInt();
            int ry = in.nextInt();
            int lz = in.nextInt();
            int rz = in.nextInt();
            long res = pref[rx][ry][rz] - pref[rx][ry][lz - 1] - pref[rx][ly - 1][rz] - pref[lx - 1][ry][rz] + pref[lx - 1][ly - 1][rz] + pref[lx - 1][ry][lz - 1] + pref[rx][ly - 1][lz - 1] - pref[lx - 1][ly - 1][lz - 1];
            out.println(res); 
        }
    }
}

Submission Info

Submission Time
Task D - Cuboid Sum Query
User SanguineChame
Language Java (OpenJDK 17)
Score 400
Code Size 10124 Byte
Status AC
Exec Time 609 ms
Memory 76160 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 22
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 02_maximum_00.txt, 02_maximum_01.txt, 02_maximum_02.txt, 02_maximum_03.txt, 02_maximum_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 36 ms 34548 KiB
00_sample_01.txt AC 37 ms 34432 KiB
01_random_00.txt AC 403 ms 65448 KiB
01_random_01.txt AC 434 ms 66072 KiB
01_random_02.txt AC 597 ms 72808 KiB
01_random_03.txt AC 409 ms 60592 KiB
01_random_04.txt AC 609 ms 76160 KiB
01_random_05.txt AC 381 ms 65424 KiB
01_random_06.txt AC 455 ms 65084 KiB
01_random_07.txt AC 583 ms 69872 KiB
01_random_08.txt AC 435 ms 67128 KiB
01_random_09.txt AC 416 ms 64400 KiB
01_random_10.txt AC 472 ms 64892 KiB
01_random_11.txt AC 450 ms 61980 KiB
01_random_12.txt AC 345 ms 61076 KiB
01_random_13.txt AC 497 ms 65304 KiB
01_random_14.txt AC 458 ms 65888 KiB
02_maximum_00.txt AC 470 ms 66376 KiB
02_maximum_01.txt AC 453 ms 66604 KiB
02_maximum_02.txt AC 446 ms 66908 KiB
02_maximum_03.txt AC 450 ms 66388 KiB
02_maximum_04.txt AC 456 ms 66236 KiB