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

Envío 6218

Problema 0x25 - Suma de un subarreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 01:46:21 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.083 s 16 KBi
#2
Correcto
0.104 s 18 KBi
#3
Correcto
0.093 s 16 KBi
#4
Correcto
0.092 s 16 KBi
#5
Correcto
0.08 s 16 KBi
#6
Correcto
0.121 s 18 KBi
#7
Correcto
0.11 s 17 KBi
#8
Correcto
0.539 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.858 s 125 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.919 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.818 s 125 KBi
#12
Correcto
0.802 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.896 s 125 KBi
#14
Correcto
0.881 s 125 KBi
Puntos totales: 72 / 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];
            int key2 = i;

            Key key = new Key(key1, key2);
            sums.put(key.hashCode(), total);
        }

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

            Key key = new Key(key1, key2);
            sums.put(key.hashCode(), total);
        }

        int c = scanner.nextInt();

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

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

    private static class Key{
        private int key1;
        private int key2;

        public Key(int key1, int key2) {
            this.key1 = key1;
            this.key2 = key2;
        }

        @Override
        public int hashCode() {
            return Objects.hash(key1, key2);
        }
    }
}