https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_ai
あらかじめ累積を計算しておくだけですね。
// How Many Guests? #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> (usize, Vec<i32>) { let v = read_vec(); let Q = v[1]; let A = read_vec(); (Q, A) } fn accumulate(A: Vec<i32>) -> Vec<i32> { let mut B: Vec<i32> = (0..A.len()+1).map(|_| 0).collect(); for (i, a) in A.into_iter().enumerate() { B[i+1] = B[i] + a } B } fn read_query() -> (usize, usize) { let v = read_vec(); (v[0], v[1]) } fn sum_range(L: usize, R: usize, B: &Vec<i32>) -> i32 { B[R] - B[L-1] } fn f(Q: usize, A: Vec<i32>) { let B = accumulate(A); for _ in 0..Q { let (L, R) = read_query(); println!("{}", sum_range(L, R, &B)) } } fn main() { let (Q, A) = read_input(); f(Q, A) }