1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! 二次元累積和
#[derive(Clone, Debug)]
pub struct CumSum2D {
    v: Vec<Vec<i64>>,
}

impl CumSum2D {
    pub fn new(source: &[Vec<i64>]) -> Self {
        let h = source.len();
        let w = source[0].len();
        let mut v = vec![vec![0i64; w + 1]; h + 1];

        for i in 0..h {
            for j in 0..w {
                v[i + 1][j + 1] = source[i][j] + v[i][j + 1] + v[i + 1][j] - v[i][j];
            }
        }
        CumSum2D { v }
    }

    pub fn query(&self, top: usize, bottom: usize, left: usize, right: usize) -> i64 {
        self.v[bottom + 1][right + 1] - self.v[bottom + 1][left] - self.v[top][right + 1]
            + self.v[top][left]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cumsum_2d() {
        let a = CumSum2D::new(&[
            vec![1, 2, 3, 4],
            vec![1, 2, 3, 4],
            vec![1, 2, 3, 4],
            vec![1, 2, 3, 4],
        ]);
        assert_eq!(
            a.v,
            vec![
                vec![0, 0, 0, 0, 0],
                vec![0, 1, 3, 6, 10],
                vec![0, 2, 6, 12, 20],
                vec![0, 3, 9, 18, 30],
                vec![0, 4, 12, 24, 40]
            ]
        );
        assert_eq!(a.query(0, 0, 0, 0), 1);
        assert_eq!(a.query(0, 1, 0, 1), 6);
        assert_eq!(a.query(1, 2, 2, 3), 14);
        assert_eq!(a.query(0, 0, 0, 3), 10);
        assert_eq!(a.query(0, 3, 0, 0), 4);
        assert_eq!(a.query(3, 3, 3, 3), 4);

        assert_eq!(a.query(0, 3, 0, 3), 40);
    }
}