アルゴリズムと数学 095

https://atcoder.jp/contests/math-and-algorithm/tasks/typical90_j

累積を取るパターンですが、1組と2組の2つを取るところが特殊ですね。

// Score Sum Queries
#![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() -> (Vec<(i32, i32)>, usize) {
    let N: usize = read();
    let students: Vec<(i32, i32)> = (0..N).map(|_| read_vec::<i32>()).
                                           map(|v| (v[0], v[1])).collect();
    let Q: usize = read();
    (students, Q)
}

fn accumulate(students: Vec<(i32, i32)>) -> Vec<(i32, i32)> {
    let N = students.len();
    let mut acc: Vec<(i32, i32)> = vec![(0, 0); N+1];
    for (i, (C, P)) in students.into_iter().enumerate() {
        acc[i+1] = match C {
                    1 => (acc[i].0+P, acc[i].1),
                    _ => (acc[i].0, acc[i].1+P)
        };
    }
    acc
}

fn read_query() -> (usize, usize) {
    let v = read_vec();
    (v[0], v[1])
}

fn f(students: Vec<(i32, i32)>, Q: usize) {
    let acc = accumulate(students);
    for _ in 0..Q {
        let (L, R) = read_query();
        let A = acc[R].0 - acc[L-1].0;
        let B = acc[R].1 - acc[L-1].1;
        println!("{} {}", A, B)
    }
}

fn main() {
    let (students, Q) = read_input();
    f(students, Q)
}