Problem solution · Python

ARC205 A — 2x2 Erasing

ARC205 A — 2x2 Erasing: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
77 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ARC205 A — 2x2 Erasing, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. 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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeARC205 A — 2x2 Erasing · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- from typing import Any  # See:# https://atcoder.jp/contests/abc311/submissions/43843595class CumulativeSum2d:    def __init__(self, array: list[list[Any]]) -> None:        self.height: int = len(array)        self.width: int = len(array[0])        self.summed_array: list[list[Any]] = [            [0] * (self.width + 1) for _ in range(self.height + 1)        ]         for i in range(self.height):            for j in range(self.width):                self.summed_array[i + 1][j + 1] = (                    self.summed_array[i + 1][j]                    + self.summed_array[i][j + 1]                    - self.summed_array[i][j]                    + array[i][j]                )     def query(self, x1: int, y1: int, x2: int, y2: int) -> Any:        # x1, y1, x2, y2: 0-indexed        assert 0 <= x1 <= x2 <= self.width        assert 0 <= y1 <= y2 <= self.height         return (            self.summed_array[y2][x2]            - self.summed_array[y1][x2]            - self.summed_array[y2][x1]            + self.summed_array[y1][x1]        )     def get_summed_array(self) -> list[list[Any]]:        return self.summed_array  def main():    import sys     input = sys.stdin.readline     n, q = map(int, input().split())    s = [list(input().rstrip()) for _ in range(n)]     size = 5 * 10**2  # FIXME: Update value if needs.    array = [[0 for _ in range(size + 10)] for _ in range(size + 10)]     # 2 * 2 の白マスが置けるか判定し、その場合は左上に置く    for i in range(n):        for j in range(n):            if i + 1 >= n or j + 1 >= n:                continue            if (                s[i][j] == "#"                or s[i][j + 1] == "#"                or s[i + 1][j] == "#"                or s[i + 1][j + 1] == "#"            ):                continue             array[i][j] += 1     c = CumulativeSum2d(array)     for _ in range(q):        ui, di, li, ri = map(int, input().split())        ans = c.query(li - 1, ui - 1, ri - 1, di - 1)        print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗