結果

問題 No.1093 区間の和 / Sum of Range
ユーザー fukafukatanifukafukatani
提出日時 2020-10-24 20:06:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 2,239 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 151,388 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 20:43:35
合計ジャッジ時間 9,343 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 75 ms
4,376 KB
testcase_02 AC 36 ms
4,376 KB
testcase_03 AC 13 ms
4,380 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 115 ms
4,376 KB
testcase_06 AC 21 ms
4,376 KB
testcase_07 AC 60 ms
4,380 KB
testcase_08 AC 44 ms
4,376 KB
testcase_09 AC 101 ms
4,380 KB
testcase_10 AC 51 ms
4,380 KB
testcase_11 AC 104 ms
4,376 KB
testcase_12 AC 57 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 114 ms
4,376 KB
testcase_15 AC 24 ms
4,380 KB
testcase_16 AC 129 ms
4,376 KB
testcase_17 AC 113 ms
4,380 KB
testcase_18 AC 72 ms
4,376 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 60 ms
4,380 KB
testcase_21 AC 132 ms
4,380 KB
testcase_22 AC 124 ms
4,376 KB
testcase_23 AC 94 ms
4,380 KB
testcase_24 AC 15 ms
4,376 KB
testcase_25 AC 113 ms
4,380 KB
testcase_26 AC 76 ms
4,380 KB
testcase_27 AC 120 ms
4,380 KB
testcase_28 AC 72 ms
4,380 KB
testcase_29 AC 143 ms
4,376 KB
testcase_30 AC 18 ms
4,376 KB
testcase_31 AC 22 ms
4,376 KB
testcase_32 AC 37 ms
4,380 KB
testcase_33 AC 122 ms
4,380 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 132 ms
4,384 KB
testcase_36 AC 100 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![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
    }
}
0