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

Envío 6242

Problema 0xca - Contar cuantas veces aparece X en un subarreglo

  • Autor: rpedrazacoello
  • Fecha: 2022-05-27 02:45:06 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.122 s 18 KBi
#2
Correcto
0.092 s 16 KBi
#3
Correcto
0.297 s 39 KBi
#4
Tiempo límite excedido
1.094 s 104 KBi
#5
Tiempo límite excedido
1.051 s 83 KBi
#6
Tiempo límite excedido
1.065 s 125 KBi
#7
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.665 s 125 KBi
#8
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.737 s 125 KBi
Puntos totales: 38 / 100

Código

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

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

        for(int i=0; i<n; i++){
            int value = scanner.nextInt();
            if(!memoization.containsKey(value)){
                ArrayList<Integer> arrayList = new ArrayList<Integer>();
                arrayList.add(i);
                memoization.put(value, arrayList);
            } else {
                memoization.get(value).add(i);
            }
        }

        int c = scanner.nextInt();

        for(int i=0; i<c; i++){
            int l = scanner.nextInt();
            int r = scanner.nextInt();
            int x = scanner.nextInt();

            int count = 0;
            if(memoization.containsKey(x)) {
                ArrayList<Integer> arrayList = memoization.get(x);
                for (int j = 0; j < arrayList.size(); j++) {
                    int value = arrayList.get(j);
                    if (value >= l && value <= r) {
                        count++;
                    }
                }
            }
            System.out.println(count);
        }
    }
}