- 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
- 72 lines of Python from the credited upstream file abc318_b.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 Any, List4 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 = int(input())47 size = 10**2 48 array = [[0 for _ in range(size + 10)] for _ in range(size + 10)]49 50 for _ in range(n):51 ai, bi, ci, di = map(int, input().split())52 53 array[ci][ai] += 154 array[ci][bi] -= 155 array[di][ai] -= 156 array[di][bi] += 157 58 c = CumulativeSum2d(array)59 grid = c.get_summed_array()60 ans = 061 62 for i in range(size + 1):63 for j in range(size + 1):64 if grid[i][j] >= 1:65 ans += 166 67 print(ans)68 69 70if __name__ == "__main__":71 main()72