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

Envío 6220

Problema 0x25 - Suma de un subarreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 01:58:43 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.142 s 14 KBi
#2
Correcto
0.081 s 16 KBi
#3
Correcto
0.09 s 16 KBi
#4
Correcto
0.098 s 16 KBi
#5
Correcto
0.143 s 14 KBi
#6
Correcto
0.106 s 17 KBi
#7
Correcto
0.103 s 17 KBi
#8
Correcto
0.561 s 56 KBi
#9
Tiempo límite excedido
1.151 s 107 KBi
#10
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.69 s 125 KBi
#11
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.58 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.481 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.572 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.672 s 125 KBi
Puntos totales: 58 / 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();

        int[][] consultas = new int[c][2];
        for(int i=0; i<c; i++){
            int left = scanner.nextInt();
            int right = scanner.nextInt();
            consultas[i][0]=left;
            consultas[i][1]=right;
        }
        
        for(int i=0; i<c; i++){
            int left = consultas[i][0];
            int right = consultas[i][1];
            
            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);
        }
    }
}