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

Envío 5019

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

  • Autor: Ikerlb
  • Fecha: 2021-10-06 20:22:31 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.18 s 40 KBi
#2
Correcto
0.117 s 34 KBi
#3
Correcto
0.231 s 15 KBi
#4
Correcto
0.937 s 48 KBi
#5
Tiempo límite excedido
1.183 s 55 KBi
#6
Tiempo límite excedido
1.039 s 70 KBi
#7
Tiempo límite excedido
1.076 s 53 KBi
#8
Tiempo límite excedido
1.06 s 68 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.io.BufferedOutputStream;
import java.io.OutputStream;


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));
        Map<Integer, List<Integer>> byElems = new HashMap<>();
        List<String> lines = new ArrayList<String>();
        String line;

        while((line = reader.readLine()) != null)
            lines.add(line);    

        // First line is array size 
        line = lines.get(0);
        StringTokenizer tokenizer = new StringTokenizer(line);
        int N = Integer.valueOf(tokenizer.nextToken());

        // Second line is array itself    
        line = lines.get(1);
        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 = lines.get(2);
        tokenizer = new StringTokenizer(line);
        int Q = Integer.valueOf(tokenizer.nextToken()); 
        int L, R, X, left, right, j;
        L = R = X = left = right = 0;
        j = 3;
        OutputStream out = new BufferedOutputStream ( System.out );
        for(int i = 0; i < Q; i++){
            // a line per query
            line = lines.get(j);
            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);
            out.write((right - left + "\n").getBytes());
            j += 1;
        }
        out.flush();
    }
}