• + 0 comments

    Hi there!

    Don't use stupid main function provided by Hackerrank for Java.

    It is extremly slow! Just load of test case exceed time limit on test case 4+.

    Beware! Rewrite it:

        public static void main(String[] args) throws IOException {
            final Scanner scanner = new Scanner(System.in);
            final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int n = scanner.nextInt();
            int q = scanner.nextInt();
            int[] values = new int[n];
            for (int i = 0; i < n; ++i) {
                values[i] = scanner.nextInt();
            }
            int[][] tree = new int[n - 1][2];
            for (int i = 0; i < n - 1; ++i) {
                tree[i][0] = scanner.nextInt();
                tree[i][1] = scanner.nextInt();
            }
            int[][] queries = new int[q][4];
            for (int i = 0; i < q; ++i) {
                queries[i][0] = scanner.nextInt();
                queries[i][1] = scanner.nextInt();
                queries[i][2] = scanner.nextInt();
                queries[i][3] = scanner.nextInt();
            }
    
            int[] result = solve(values, tree, queries);
    
            for (int i : result) {
                bufferedWriter.write(String.valueOf(i));
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
            scanner.close();
        }