█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6263

Problema 0x25 - Suma de un subarreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 17:14:28 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.082 s 16 KBi
#2
Correcto
0.138 s 14 KBi
#3
Correcto
0.094 s 18 KBi
#4
Correcto
0.086 s 16 KBi
#5
Correcto
0.077 s 16 KBi
#6
Correcto
0.154 s 18 KBi
#7
Correcto
0.101 s 17 KBi
#8
Correcto
0.552 s 58 KBi
#9
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.868 s 125 KBi
#10
Tiempo límite excedido
1.163 s 86 KBi
#11
Correcto
0.778 s 125 KBi
#12
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.929 s 125 KBi
#13
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.904 s 125 KBi
#14
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.959 s 125 KBi
Puntos totales: 65 / 100

Código

import java.util.HashMap;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        HashMap<Integer, Integer> sums = new HashMap<>();

        int key1 = 0;
        int total = 0;
        for(int i=0; i<n; i++){
            array[i]=scanner.nextInt();
            total += array[i];

            sums.put(Objects.hash(key1, i), total);
        }

        total = 0;
        int key2 = n-1;
        for(int i=n-1; i>=0; i--){
            total+=array[i];
            key1 = i;

            sums.put(Objects.hash(key1, key2), total);
        }

        int c = scanner.nextInt();

        for(int i=0; i<c; i++){
            int left = scanner.nextInt();
            int right = scanner.nextInt();
            total = sums.get(Objects.hash(0, n-1));
            int sumLeft = left!= 0 ? sums.get(Objects.hash(0, left-1)) : 0;
            int sumRight = right != n-1 ? sums.get(Objects.hash(right+1, n-1)) : 0;

            System.out.println(total - sumLeft - sumRight);
        }
    }
}