結果

問題 No.2758 RDQ
ユーザー tenten
提出日時 2024-08-05 10:13:02
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 2,641 bytes
コンパイル時間 3,429 ms
コンパイル使用メモリ 97,560 KB
実行使用メモリ 121,168 KB
最終ジャッジ日時 2024-08-05 10:13:17
合計ジャッジ時間 12,367 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 6 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        Map<Integer, TreeMap<Integer, Integer>> places = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();
            for (int j = 1; j <= Math.sqrt(x); j++) {
                if (x % j == 0) {
                    if (!places.containsKey(j)) {
                        places.put(j, new TreeMap<>());
                    }
                    places.get(j).put(i, null);
                    if (j * j == x) {
                        continue;
                    }
                    if (!places.containsKey(x / j)) {
                        places.put(x / j, new TreeMap<>());
                    }
                    places.get(x / j).put(i, null);
                }
            }
        }
        for (TreeMap<Integer, Integer> current : places.values()) {
            int idx = 0;
            for (int x : current.keySet()) {
                current.put(x, idx++);
            }
        }
        String result = IntStream.range(0, q).mapToObj(i -> {
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            int x = sc.nextInt();
            if (!places.containsKey(x) || places.get(x).firstKey() > right || places.get(x).lastKey() < left) {
                return "0";
            } else {
                return String.valueOf(places.get(x).floorEntry(right).getValue() - places.get(x).ceilingEntry(left).getValue() + 1);
            }
        }).collect(Collectors.joining("\n"));
        System.out.println(result);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0