Problem solution · Python

ABC182 E — Akari

ABC182 E — Akari: 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
92 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 ABC182 E — Akari, 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

  • 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.

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 codeABC182 E — Akari · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    import sys     input = sys.stdin.readline     h, w, n, m = map(int, input().split())    # -1: Block    #  0: Not used    #  1: On    grid = [[0 for _ in range(w)] for _ in range(h)]    results = [[0 for _ in range(w)] for _ in range(h)]     # See:    # https://atcoder.jp/contests/abc182/submissions/17966314    for i in range(n):        ai, bi = map(int, input().split())        ai -= 1        bi -= 1        grid[ai][bi] = 1     for j in range(m):        ci, di = map(int, input().split())        ci -= 1        di -= 1        grid[ci][di] = -1     # Right    for i in range(h):        # フラグの用意        on = 0         for j in range(w):            # 光源かブロックか判定して、フラグを変更            if grid[i][j] == 1:                on = 1            elif grid[i][j] == -1:                on = 0             # 論理和(OR)            results[i][j] |= on     # Left    for i in range(h):        on = 0         for j in range(w - 1, -1, -1):            if grid[i][j] == 1:                on = 1            elif grid[i][j] == -1:                on = 0             results[i][j] |= on     # Down    for j in range(w):        on = 0         for i in range(h):            if grid[i][j] == 1:                on = 1            elif grid[i][j] == -1:                on = 0             results[i][j] |= on     # Up    for j in range(w):        on = 0         for i in range(h - 1, -1, -1):            if grid[i][j] == 1:                on = 1            elif grid[i][j] == -1:                on = 0             results[i][j] |= on     ans = 0     for i in range(h):        for j in range(w):            ans += results[i][j]     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 ↗