結果
問題 |
No.1093 区間の和 / Sum of Range
|
ユーザー |
![]() |
提出日時 | 2020-10-24 20:06:01 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 128 ms / 2,000 ms |
コード長 | 2,239 bytes |
コンパイル時間 | 12,580 ms |
コンパイル使用メモリ | 404,452 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-21 15:32:45 |
合計ジャッジ時間 | 17,123 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 36 |
ソースコード
#![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::io::Write; use std::ops::Bound::*; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let v = read_vec::<usize>(); let (n, k) = (v[0], v[1]); let a = read_vec::<i64>(); let q = read::<usize>(); let mut x = vec![0; q]; for i in 0..q { x[i] = read::<i64>(); } let mut s = vec![0; n - k + 1]; s[0] = (0..k).map(|x| a[x]).sum::<i64>(); for i in 1..s.len() { s[i] = s[i - 1] + a[i + k - 1] - a[i - 1]; } s.sort(); for num in x { let ans = s.upper_bound(&num); println!("{}", ans); } } fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } pub trait BinarySearch<T> { fn lower_bound(&self, x: &T) -> usize; fn upper_bound(&self, x: &T) -> usize; } impl<T: Ord> BinarySearch<T> for [T] { fn lower_bound(&self, x: &T) -> usize { let mut low = 0; let mut high = self.len(); while low != high { let mid = (low + high) / 2; match self[mid].cmp(x) { Ordering::Less => { low = mid + 1; } Ordering::Equal | Ordering::Greater => { high = mid; } } } low } fn upper_bound(&self, x: &T) -> usize { let mut low = 0; let mut high = self.len(); while low != high { let mid = (low + high) / 2; match self[mid].cmp(x) { Ordering::Less | Ordering::Equal => { low = mid + 1; } Ordering::Greater => { high = mid; } } } low } }