結果
| 問題 | No.3227 Matrix Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-01-02 16:06:34 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 991 ms / 8,000 ms |
| コード長 | 3,017 bytes |
| 記録 | |
| コンパイル時間 | 320 ms |
| コンパイル使用メモリ | 82,652 KB |
| 実行使用メモリ | 120,608 KB |
| 最終ジャッジ日時 | 2026-01-02 16:06:57 |
| 合計ジャッジ時間 | 22,992 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 28 |
ソースコード
## https://yukicoder.me/problems/no/3227
from collections import deque
class SegmentTree:
"""
非再帰版セグメント木。
更新は「加法」、取得は「最大値」のもの限定。
"""
def __init__(self, init_array, mod):
self.mod = mod
n = 1
while n < len(init_array):
n *= 2
self.size = n
self.array = [[0] * 4 for _ in range(2 * self.size)]
for i, a in enumerate(init_array):
for j in range(4):
self.array[self.size + i][j] = a[j]
end_index = self.size
start_index = end_index // 2
while start_index >= 1:
for i in range(start_index, end_index):
self._op(self.array[i], self.array[2 * i], self.array[2 * i + 1])
end_index = start_index
start_index = end_index // 2
def _op(self, array, left, right):
array[0] = left[0] * right[0] + left[1] * right[2]
array[0] %= self.mod
array[1] = left[0] * right[1] + left[1] * right[3]
array[1] %= self.mod
array[2] = left[2] * right[0] + left[3] * right[2]
array[2] %= self.mod
array[3] = left[2] * right[1] + left[3] * right[3]
array[3] %= self.mod
def set(self, x, a):
index = self.size + x
for j in range(4):
self.array[index][j] = a[j]
while index > 1:
index //= 2
self._op(self.array[index], self.array[2 * index], self.array[2 * index + 1])
def get_prod(self, l, r):
L = self.size + l; R = self.size + r
# 2. 区間[l, r)の最大値を求める
l_queue = deque()
r_queue = deque()
while L < R:
if R & 1:
R -= 1
r_queue.appendleft(self.array[R])
if L & 1:
l_queue.append(self.array[L])
L += 1
L >>= 1; R >>= 1
s = [1, 0, 0, 1]
while len(l_queue) > 0:
new_s = [0] * 4
x = l_queue.popleft()
self._op(new_s, s, x)
s = new_s
while len(r_queue) > 0:
new_s = [0] * 4
x = r_queue.popleft()
self._op(new_s, s, x)
s = new_s
return s
def main():
K, N = map(int, input().split())
X = []
for _ in range(N):
a, b = map(int, input().split())
c, d = map(int, input().split())
X.append((a, b, c, d))
Q = int(input())
queries = []
for _ in range(Q):
i, l, r = map(int, input().split())
a, b = map(int, input().split())
c, d = map(int, input().split())
queries.append((i - 1, l - 1 ,r - 1, (a, b, c, d)))
seg_tree = SegmentTree(X, K)
for i, l, r, matrix in queries:
seg_tree.set(i, matrix)
x = seg_tree.get_prod(l, r + 1)
print(x[0] % K, x[1] % K)
print(x[2] % K, x[3] % K)
if __name__ == "__main__":
main()