Skip to content

Commit

Permalink
Merge pull request #4795 from freakin23/master
Browse files Browse the repository at this point in the history
py sol for ac-nuske
  • Loading branch information
SansPapyrus683 committed Sep 21, 2024
2 parents 2652a1f + 59e0954 commit 6400b18
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions solutions/silver/ac-nuske.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,74 @@ public class Main {
```

</JavaSection>
<PySection>

```py
MAX_SIZE = 2000

n, m, q = map(int, input().split())

grid = [[False] * (MAX_SIZE + 1) for _ in range(MAX_SIZE + 1)]

# Read the grid
for i in range(1, n + 1):
row = input()
for j in range(1, m + 1):
grid[i][j] = row[j - 1] == "1"

# Overall prefix sums array. pref[x][y] is how many components
# we have in the 2D array from (1, 1) to (x, y), inclusive.
pref = [[0] * (MAX_SIZE + 1) for _ in range(MAX_SIZE + 1)]

for i in range(1, n + 1):
for j in range(1, m + 1):
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1]
if grid[i][j]:
"""
0
0 1
A new connected component forms!
"""
if not grid[i - 1][j] and not grid[i][j - 1]:
pref[i][j] += 1

"""
1
1 1
Two connected componets merge!
"""
if grid[i - 1][j] and grid[i][j - 1]:
pref[i][j] -= 1

# horpref[x][y]: number of connected components in the row from (x, 1) to (x, y)
horpref = [[0] * (MAX_SIZE + 1) for _ in range(MAX_SIZE + 1)]

# verpref[x][y]: number of connected components in the column from (1, y) to (x, y)
verpref = [[0] * (MAX_SIZE + 1) for _ in range(MAX_SIZE + 1)]

for i in range(1, n + 1):
for j in range(1, m + 1):
horpref[i][j] = horpref[i][j - 1]
verpref[i][j] = verpref[i - 1][j]
if grid[i][j]:
horpref[i][j] += not grid[i][j - 1]
verpref[i][j] += not grid[i - 1][j]

for _ in range(q):
a, b, c, d = map(int, input().split())

# Whether we start with a connected component
ans = grid[a][b]

# How many new components appear in the top row and leftmost column
ans += horpref[a][d] - horpref[a][b]
ans += verpref[c][b] - verpref[a][b]

# Change in the number of connected components in the rest of the grid
ans += pref[c][d] - pref[a][d] - pref[c][b] + pref[a][b]

print(ans)
```

</PySection>
</LanguageSection>

0 comments on commit 6400b18

Please sign in to comment.