結果

問題 No.2758 RDQ
ユーザー Yukino DX.Yukino DX.
提出日時 2024-05-18 09:56:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 491 ms / 2,000 ms
コード長 2,453 bytes
コンパイル時間 13,198 ms
コンパイル使用メモリ 402,404 KB
実行使用メモリ 165,436 KB
最終ジャッジ日時 2024-05-18 09:57:14
合計ジャッジ時間 22,720 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 392 ms
165,068 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 465 ms
161,280 KB
testcase_07 AC 464 ms
161,280 KB
testcase_08 AC 467 ms
161,280 KB
testcase_09 AC 491 ms
161,408 KB
testcase_10 AC 468 ms
161,280 KB
testcase_11 AC 452 ms
165,304 KB
testcase_12 AC 419 ms
165,304 KB
testcase_13 AC 420 ms
165,304 KB
testcase_14 AC 444 ms
165,436 KB
testcase_15 AC 436 ms
165,436 KB
testcase_16 AC 420 ms
165,300 KB
testcase_17 AC 450 ms
165,300 KB
testcase_18 AC 419 ms
165,432 KB
testcase_19 AC 436 ms
165,304 KB
testcase_20 AC 442 ms
165,304 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashMap;

use proconio::{input, marker::Usize1};

fn main() {
    input! {
        n:usize,
        q:usize,
        a:[usize;n],
        lrk:[(Usize1,Usize1,usize);q],
    }

    const M: usize = 400;
    let mut cum = vec![vec![0; n]; M];
    for i in 1..M {
        for j in 0..n {
            if a[j] % i == 0 {
                cum[i][j] = 1;
            }
        }
    }

    for i in 0..M {
        cum[i] = cum[i].cum();
    }

    let mut is = HashMap::new();
    for i in 0..n {
        if a[i] < M {
            continue;
        }
        is.entry(a[i]).or_insert(vec![]).push(i);
    }

    for (l, r, k) in lrk {
        let ans = if k < M {
            cum[k][r + 1] - cum[k][l]
        } else {
            let mut ans = 0;
            for i in (1..).take_while(|&i| k * i <= 100000) {
                if let Some(is) = is.get(&(i * k)) {
                    ans += is.lower_bound(r + 1) - is.lower_bound(l);
                }
            }

            ans
        };

        println!("{}", ans);
    }
}

use cumulative::*;
mod cumulative {

    pub trait Cumulative {
        fn cum(&self) -> Vec<usize>;
    }

    impl Cumulative for [usize]
    {
        //cum[i]:=sum[0,i)
        fn cum(&self) -> Vec<usize> {
            let n = self.len();
            let mut res = vec![0; n + 1];
            for i in 0..n {
                res[i + 1] = res[i] + self[i];
            }

            res
        }
    }
}

use bound::*;
mod bound {
    pub trait Bound<T> {
        fn lower_bound(&self, val: T) -> usize;
        fn upper_bound(&self, val: T) -> usize;
    }

    impl<T> Bound<T> for [T]
    where
        T: Ord + Copy,
    {
        fn lower_bound(&self, val: T) -> usize {
            let mut ok = self.len();
            let mut ng = !0;
            while ok.wrapping_sub(ng) > 1 {
                let m = ok.wrapping_add(ng) / 2;
                if val <= self[m] {
                    ok = m;
                } else {
                    ng = m;
                }
            }

            ok
        }

        fn upper_bound(&self, val: T) -> usize {
            let mut ok = !0;
            let mut ng = self.len();
            while ng.wrapping_sub(ok) > 1 {
                let m = ng.wrapping_add(ok) / 2;
                if self[m] <= val {
                    ok = m;
                } else {
                    ng = m;
                }
            }

            ok
        }
    }
}
0