- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 68 lines of Python from the credited upstream file abc366_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4from typing import Any, List5 6 789class CumulativeSum2d:10 def __init__(self, array: List[List[Any]]) -> None:11 self.height: int = len(array)12 self.width: int = len(array[0])13 self.summed_array: List[List[Any]] = [14 [0] * (self.width + 1) for _ in range(self.height + 1)15 ]16 17 for i in range(self.height):18 for j in range(self.width):19 self.summed_array[i + 1][j + 1] = (20 self.summed_array[i + 1][j]21 + self.summed_array[i][j + 1]22 - self.summed_array[i][j]23 + array[i][j]24 )25 26 def query(self, x1: int, y1: int, x2: int, y2: int) -> Any:27 28 assert 0 <= x1 <= x2 <= self.width29 assert 0 <= y1 <= y2 <= self.height30 31 return (32 self.summed_array[y2][x2]33 - self.summed_array[y1][x2]34 - self.summed_array[y2][x1]35 + self.summed_array[y1][x1]36 )37 38 def get_summed_array(self) -> List[List[Any]]:39 return self.summed_array40 41 42def main():43 import sys44 45 input = sys.stdin.readline46 47 n = int(input())48 cs = []49 50 for _ in range(n):51 array = [list(map(int, input().split())) for _ in range(n)]52 cs.append(CumulativeSum2d(array))53 54 q = int(input())55 56 for _ in range(q):57 lxi, rxi, lyi, ryi, lzi, rzi = map(int, input().split())58 ans = 059 60 for x in range(lxi - 1, rxi):61 ans += cs[x].query(lzi - 1, lyi - 1, rzi, ryi)62 63 print(ans)64 65 66if __name__ == "__main__":67 main()68