- 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
- 92 lines of Python from the credited upstream file abc182_e.py.
- The implementation visibly relies on 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 4def main():5 import sys6 7 input = sys.stdin.readline8 9 h, w, n, m = map(int, input().split())10 11 12 13 grid = [[0 for _ in range(w)] for _ in range(h)]14 results = [[0 for _ in range(w)] for _ in range(h)]15 16 17 18 for i in range(n):19 ai, bi = map(int, input().split())20 ai -= 121 bi -= 122 grid[ai][bi] = 123 24 for j in range(m):25 ci, di = map(int, input().split())26 ci -= 127 di -= 128 grid[ci][di] = -129 30 31 for i in range(h):32 33 on = 034 35 for j in range(w):36 37 if grid[i][j] == 1:38 on = 139 elif grid[i][j] == -1:40 on = 041 42 43 results[i][j] |= on44 45 46 for i in range(h):47 on = 048 49 for j in range(w - 1, -1, -1):50 if grid[i][j] == 1:51 on = 152 elif grid[i][j] == -1:53 on = 054 55 results[i][j] |= on56 57 58 for j in range(w):59 on = 060 61 for i in range(h):62 if grid[i][j] == 1:63 on = 164 elif grid[i][j] == -1:65 on = 066 67 results[i][j] |= on68 69 70 for j in range(w):71 on = 072 73 for i in range(h - 1, -1, -1):74 if grid[i][j] == 1:75 on = 176 elif grid[i][j] == -1:77 on = 078 79 results[i][j] |= on80 81 ans = 082 83 for i in range(h):84 for j in range(w):85 ans += results[i][j]86 87 print(ans)88 89 90if __name__ == "__main__":91 main()92