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

Envío 4983

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

  • Autor: Ikerlb
  • Fecha: 2021-10-04 19:32:46 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.233 s 11 KBi
#2
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.12 s 12 KBi
#3
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.203 s 11 KBi
#4
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.194 s 11 KBi
#5
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.109 s 11 KBi
#6
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.089 s 11 KBi
#7
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.33 s 11 KBi
#8
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
0.102 s 11 KBi
Puntos totales: 0 / 100

Código

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

class Sol{
    public static int query(List<Integer> arr, int target){
        if(arr == null || target < 0)   
            return 0;
        int l = 0;              
        int r = arr.size() - 1;
        int res = -1;

        while(l <= r){
            int m = (l + r) >> 1; // safe because of bounds     
            if(arr.get(m) <= target){
                res = m;                    
                l = m + 1;
            } else {
                r = m - 1;      
            }   
        }
        return res + 1;
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);   
        int N = scanner.nextInt();
        Map<Integer, List<Integer>> byElems = new HashMap<>();      

        for(int i = 0; i < N; i++){ 
            int elem = scanner.nextInt();               
            List<Integer> m = byElems.get(elem);
            if(m == null){  
                m = new ArrayList<>();
                byElems.put(elem, m);           
            }
            m.add(i);
        }

        int Q = scanner.nextInt();      
        int L, R, X, left, right;
        L = R = X = left = right = 0;
        for(int i = 0; i < Q; i++){
            L = scanner.nextInt();
            R = scanner.nextInt();
            X = scanner.nextInt();          
            left = query(byElems.get(X), L - 1);
            right = query(byElems.get(X), R);
            System.out.println(right - left);
        }
    }
}