- 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
- 77 lines of Python from the credited upstream file arc205_a.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 3from typing import Any4 5 678class CumulativeSum2d:9 def __init__(self, array: list[list[Any]]) -> None:10 self.height: int = len(array)11 self.width: int = len(array[0])12 self.summed_array: list[list[Any]] = [13 [0] * (self.width + 1) for _ in range(self.height + 1)14 ]15 16 for i in range(self.height):17 for j in range(self.width):18 self.summed_array[i + 1][j + 1] = (19 self.summed_array[i + 1][j]20 + self.summed_array[i][j + 1]21 - self.summed_array[i][j]22 + array[i][j]23 )24 25 def query(self, x1: int, y1: int, x2: int, y2: int) -> Any:26 27 assert 0 <= x1 <= x2 <= self.width28 assert 0 <= y1 <= y2 <= self.height29 30 return (31 self.summed_array[y2][x2]32 - self.summed_array[y1][x2]33 - self.summed_array[y2][x1]34 + self.summed_array[y1][x1]35 )36 37 def get_summed_array(self) -> list[list[Any]]:38 return self.summed_array39 40 41def main():42 import sys43 44 input = sys.stdin.readline45 46 n, q = map(int, input().split())47 s = [list(input().rstrip()) for _ in range(n)]48 49 size = 5 * 10**2 50 array = [[0 for _ in range(size + 10)] for _ in range(size + 10)]51 52 53 for i in range(n):54 for j in range(n):55 if i + 1 >= n or j + 1 >= n:56 continue57 if (58 s[i][j] == "#"59 or s[i][j + 1] == "#"60 or s[i + 1][j] == "#"61 or s[i + 1][j + 1] == "#"62 ):63 continue64 65 array[i][j] += 166 67 c = CumulativeSum2d(array)68 69 for _ in range(q):70 ui, di, li, ri = map(int, input().split())71 ans = c.query(li - 1, ui - 1, ri - 1, di - 1)72 print(ans)73 74 75if __name__ == "__main__":76 main()77