Skip to content

Commit 7336350

Browse files
committed
feat: 双対セグ木ライブラリを作成
1 parent f07e949 commit 7336350

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

libs/dual_segtree.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import Any, Callable
2+
3+
4+
class DualSegmentTree:
5+
def __init__(self, op: Callable[[Any, Any], Any], e: Any, n: int) -> None:
6+
"""
7+
区間作用/一点取得のセグメント木
8+
opは区間作用用の関数
9+
eは初期値
10+
vは長さ
11+
"""
12+
self._op: Callable[[Any, Any], Any] = op
13+
self._e: Any = e
14+
self._n: int = n
15+
self.n: int = 1 << (n - 1).bit_length()
16+
self.data = [e] * (self.n * 2)
17+
18+
def apply(self, l, r, x) -> None:
19+
"""
20+
区間[l,r)にxを適用
21+
"""
22+
l += self.n
23+
r += self.n
24+
25+
while l < r:
26+
if l & 1:
27+
self.data[l] = self._op(self.data[l], x)
28+
l += 1
29+
30+
if r & 1:
31+
self.data[r - 1] = self._op(self.data[r - 1], x)
32+
33+
l >>= 1
34+
r >>= 1
35+
36+
def get(self, p: int) -> Any:
37+
"""
38+
pの値を取得する
39+
"""
40+
41+
res = self._e
42+
p += self.n
43+
44+
while p:
45+
res = self._op(res, self.data[p])
46+
p >>= 1
47+
48+
return res

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" "lca_weight.py" "rollinghash.py" "graph.py" "unionfind.py" "potential_unionfind.py" "heap.py" "trie.py" "mo.py" "square-division.py" "bit.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" "lca_weight.py" "rollinghash.py" "graph.py" "unionfind.py" "potential_unionfind.py" "heap.py" "trie.py" "mo.py" "square-division.py" "bit.py" "dual_segtree.py" "dis_lib.py" "alias.py" "utils.py"; do
1111

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

mise.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ run = "./pre_commit.bash"
1010

1111
[env]
1212
_.python.venv = { path = ".venv" }
13-
PYTHON_PATH = "."
13+
PYTHONPATH = "."
14+

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8-
"debugpy>=1.8.14",
8+
"debugpy>=1.8.14",
99
]
1010

1111
[tool.ruff]
1212
line-length = 88
1313

1414
[tool.ruff.lint]
1515
select = ["C9", "E", "F", "W", "I", "ANN"]
16+
ignore = ["E741", "ANN001"]
17+

tests/ABC408C.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from libs.dual_segtree import DualSegmentTree
2+
3+
N, M = map(int, input().split())
4+
seg = DualSegmentTree(lambda a, b: a + b, 0, N)
5+
6+
for _ in [0] * M:
7+
l, r = map(int, input().split())
8+
seg.apply(l - 1, r, 1)
9+
10+
ans = 1000000000000000000
11+
12+
for i in range(N):
13+
ans = min(ans, seg.get(i))
14+
15+
print(ans)

0 commit comments

Comments
 (0)