結果
| 問題 | No.2758 RDQ |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-18 09:56:50 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 473 ms / 2,000 ms |
| コード長 | 2,453 bytes |
| 記録 | |
| コンパイル時間 | 13,695 ms |
| コンパイル使用メモリ | 395,944 KB |
| 実行使用メモリ | 165,432 KB |
| 最終ジャッジ日時 | 2024-12-20 16:15:19 |
| 合計ジャッジ時間 | 22,536 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
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
}
}
}