Skip to content

Commit a5ff436

Browse files
committed
feat: 二次元累積和ライブラリを作成
1 parent 3c4d9ad commit a5ff436

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

code/main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,40 @@ def update(self, i: int, x: Any) -> None:
15851585
self.blocks[block_ind] = self.op(self.blocks[block_ind], self.lis[i])
15861586

15871587

1588+
class PrefixSum2D:
1589+
def __init__(self, h: int, w: int) -> None:
1590+
self.data = [[0] * (w + 1) for _ in [0] * (h + 1)]
1591+
self.builded = False
1592+
self.h = h
1593+
self.w = w
1594+
1595+
def add(self, x: int, y: int, a: int) -> None:
1596+
assert 0 <= x < self.h and 0 <= y < self.w
1597+
1598+
self.data[x + 1][y + 1] += a
1599+
1600+
def build(self) -> None:
1601+
assert not self.builded
1602+
1603+
for i in range(self.h + 1):
1604+
for k in range(self.w):
1605+
self.data[i][k + 1] += self.data[i][k]
1606+
1607+
for k in range(self.w + 1):
1608+
for i in range(self.h):
1609+
self.data[i + 1][k] += self.data[i][k]
1610+
1611+
def prod(self, ax: int, ay: int, bx: int, by: int) -> int:
1612+
assert 0 <= ax <= bx < self.h and 0 <= ay <= by < self.w
1613+
1614+
return (
1615+
self.data[bx + 1][by + 1]
1616+
+ self.data[ax][ay]
1617+
- self.data[ax][by + 1]
1618+
- self.data[bx + 1][ay]
1619+
)
1620+
1621+
15881622
from typing import Any, Callable
15891623

15901624

libs/prefix_sum_2d.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class PrefixSum2D:
2+
def __init__(self, h: int, w: int) -> None:
3+
self.data = [[0] * (w + 1) for _ in [0] * (h + 1)]
4+
self.builded = False
5+
self.h = h
6+
self.w = w
7+
8+
def add(self, x: int, y: int, a: int) -> None:
9+
assert 0 <= x < self.h and 0 <= y < self.w
10+
11+
self.data[x + 1][y + 1] += a
12+
13+
def build(self) -> None:
14+
assert not self.builded
15+
16+
for i in range(self.h + 1):
17+
for k in range(self.w):
18+
self.data[i][k + 1] += self.data[i][k]
19+
20+
for k in range(self.w + 1):
21+
for i in range(self.h):
22+
self.data[i + 1][k] += self.data[i][k]
23+
24+
def prod(self, ax: int, ay: int, bx: int, by: int) -> int:
25+
assert 0 <= ax <= bx < self.h and 0 <= ay <= by < self.w
26+
27+
return (
28+
self.data[bx + 1][by + 1]
29+
+ self.data[ax][ay]
30+
- self.data[ax][by + 1]
31+
- self.data[bx + 1][ay]
32+
)

merge_file.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ echo "新しいmain.py作成完了"
77
# テンプレ
88
# /bin/cat python/<filename>.py >> code/main.py
99

10-
for file_name in "import.py" "standard_input.py" "math_func.py" "array_create.py" "binary_search.py" "modint.py" "yn_func.py" "grid.py" "coordinates_to_id.py" "dijkstra.py" "get_path.py" "dp.py" "coordinate_compression.py" "memo.py" "rerooting.py" "manacher.py" "rollinghash.py" "graph.py" "unionfind.py" "euler_tour.py" "potential_unionfind.py" "heap.py" "trie.py" "mo.py" "square-division.py" "dual_segtree.py" "dis_lib.py" "alias.py" "utils.py"; do
10+
for file_name in "import.py" "standard_input.py" "math_func.py" "array_create.py" "binary_search.py" "modint.py" "yn_func.py" "grid.py" "coordinates_to_id.py" "dijkstra.py" "get_path.py" "dp.py" "coordinate_compression.py" "memo.py" "rerooting.py" "manacher.py" "rollinghash.py" "graph.py" "unionfind.py" "euler_tour.py" "potential_unionfind.py" "heap.py" "trie.py" "mo.py" "square-division.py" "prefix_sum_2d.py" "dual_segtree.py" "dis_lib.py" "alias.py" "utils.py"; do
1111

1212
cat libs/${file_name} >>code/main.py
1313
done

tests/aoj0560.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from libs.prefix_sum_2d import PrefixSum2D
2+
3+
H, W = map(int, input().split())
4+
K = int(input())
5+
S = [input() for _ in [0] * H]
6+
7+
JS, IS, OS = PrefixSum2D(H, W), PrefixSum2D(H, W), PrefixSum2D(H, W)
8+
9+
for i in range(H):
10+
for k in range(W):
11+
match S[i][k]:
12+
case "J":
13+
JS.add(i, k, 1)
14+
15+
case "O":
16+
OS.add(i, k, 1)
17+
18+
case "I":
19+
IS.add(i, k, 1)
20+
21+
JS.build()
22+
IS.build()
23+
OS.build()
24+
25+
for _ in [0] * K:
26+
ax, ay, bx, by = map(int, input().split())
27+
ax -= 1
28+
ay -= 1
29+
bx -= 1
30+
by -= 1
31+
32+
print(JS.prod(ax, ay, bx, by), OS.prod(ax, ay, bx, by), IS.prod(ax, ay, bx, by))

0 commit comments

Comments
 (0)