- 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
- 85 lines of Python from the credited upstream file abc434_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 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 = int(input())47 udlr = [tuple(map(int, input().split())) for _ in range(n)]48 49 size = 2 * 10**3 50 array = [[0 for _ in range(size + 10)] for _ in range(size + 10)]51 52 for ui, di, li, ri in udlr:53 array[ui - 1][li - 1] += 154 array[ui - 1][ri] -= 155 array[di][li - 1] -= 156 array[di][ri] += 157 58 c = CumulativeSum2d(array)59 counts = c.get_summed_array()60 total = (2 * 10**3) ** 261 ones = [[0 for _ in range(size + 10)] for _ in range(size + 10)]62 63 for i in range(size + 1):64 for j in range(size + 1):65 count = counts[i][j]66 67 if count <= 0:68 continue69 70 total -= 171 72 if count == 1:73 ones[i][j] = 174 75 c_ones = CumulativeSum2d(ones)76 77 for ui, di, li, ri in udlr:78 cur = c_ones.query(li, ui, ri + 1, di + 1)79 ans = total + cur80 print(ans)81 82 83if __name__ == "__main__":84 main()85