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

Envío 5013

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

  • Autor: Ikerlb
  • Fecha: 2021-10-06 20:01:38 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.125 s 15 KBi
#2
Correcto
0.213 s 24 KBi
#3
Correcto
0.147 s 23 KBi
#4
Tiempo límite excedido
1.085 s 41 KBi
#5
Tiempo límite excedido
1.172 s 32 KBi
#6
Tiempo límite excedido
1.08 s 36 KBi
#7
Tiempo límite excedido
1.049 s 71 KBi
#8
Correcto
0.908 s 90 KBi
Puntos totales: 50 / 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.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.StringBuilder;

class Main{
    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) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder builder = new StringBuilder(); 
        Map<Integer, List<Integer>> byElems = new HashMap<>();      

        // First line is array size 
        String line = reader.readLine();
        StringTokenizer tokenizer = new StringTokenizer(line);
        int N = Integer.valueOf(tokenizer.nextToken());

        // Second line is array itself    
        line = reader.readLine();
        tokenizer = new StringTokenizer(line);
        for(int i = 0; i < N; i++){ 
            int elem = Integer.valueOf(tokenizer.nextToken());
            List<Integer> m = byElems.get(elem);
            if(m == null){  
                m = new ArrayList<>();
                byElems.put(elem, m);           
            }
            m.add(i);
        }

        // Third line is number of queries
        line = reader.readLine();
        tokenizer = new StringTokenizer(line);
        int Q = Integer.valueOf(tokenizer.nextToken()); 
        int L, R, X, left, right;
        L = R = X = left = right = 0;
        for(int i = 0; i < Q; i++){
            // a line per query
            line = reader.readLine();
            tokenizer = new StringTokenizer(line);
            L = Integer.valueOf(tokenizer.nextToken());
            R = Integer.valueOf(tokenizer.nextToken());
            X = Integer.valueOf(tokenizer.nextToken());
            left = query(byElems.get(X), L - 1);
            right = query(byElems.get(X), R);
            builder.append(right - left);
            builder.append('\n');
        }
        System.out.println(builder.toString());
    }
}