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

Envío 4990

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

  • Autor: Ikerlb
  • Fecha: 2021-10-05 16:47:59 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.028 s 3 KBi
#2
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.031 s 3 KBi
#3
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.03 s 3 KBi
#4
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.016 s 3 KBi
#5
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.036 s 3 KBi
#6
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.029 s 3 KBi
#7
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.02 s 3 KBi
#8
Error en tiempo de ejecución (NZEC)
Exited with error status 1
  File "script.py", line 9
    class Main{ /* arr is sorted without duplicates */ 
              ^
SyntaxError: invalid syntax
0.029 s 3 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;
import java.util.Arrays;
import java.util.stream.Collectors;

class Main{ /* arr is sorted without duplicates */ 
    public static int index(List<Integer> arr, int target){
        int l = 0;
        int r = arr.size() - 1;

        while(l <= r){
            int m = (l + r) >> 1;                
            if(arr.get(m) == target)    
                return m;
            else if(arr.get(m) < target)
                l = m + 1;            
            else
                r = m - 1;
        }
        return -1;
    }

    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();
        int[] nums  = new int[N];

        for(int i = 0; i < N; i++)
            nums[i] = scanner.nextInt();                    
    
        
        int[] nnums = Arrays.copyOf(nums, nums.length);
        Arrays.sort(nnums);
        

        List<Integer> snums = new ArrayList<Integer>(); 
        int prev = Integer.MIN_VALUE;
        for(int i = 0; i < N; i++){
            if(prev != nnums[i])            
                snums.add(nnums[i]);
            prev = nnums[i];
        }


        ArrayList<ArrayList<Integer>> l = new ArrayList<ArrayList<Integer>>(snums.size());
        for(int i = 0; i < snums.size(); i++)  
            l.add(new ArrayList<Integer>());

        for(int i = 0; i < N; i++){
            int idx = index(snums, nums[i]);                 
            l.get(idx).add(i);
        }

        int Q = scanner.nextInt();      
        int L, R, X, left, right, idx;
        L = R = X = left = right = 0;
        List<Integer> m;    
        for(int i = 0; i < Q; i++){
            L = scanner.nextInt();
            R = scanner.nextInt();
            X = scanner.nextInt();
            idx = index(snums, X);
            if(idx == -1){
                System.out.println(0);
                continue;
            }
            m = l.get(idx);
            left = query(m, L - 1);
            right = query(m, R);
            System.out.println(right - left);
        }
    }
}