結果
| 問題 |
No.650 行列木クエリ
|
| コンテスト | |
| ユーザー |
konsin_tokage
|
| 提出日時 | 2018-03-06 22:22:12 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 887 ms / 2,000 ms |
| コード長 | 2,076 bytes |
| コンパイル時間 | 347 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 119,772 KB |
| 最終ジャッジ日時 | 2024-09-21 22:09:08 |
| 合計ジャッジ時間 | 4,916 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
ソースコード
import math
unit = (1,0,0,1)
def mul(x, y):
a = (x[0]*y[0]+x[1]*y[2])%(10**9+7)
b = (x[0]*y[1]+x[1]*y[3])%(10**9+7)
c = (x[2]*y[0]+x[3]*y[2])%(10**9+7)
d = (x[2]*y[1]+x[3]*y[3])%(10**9+7)
return (a,b,c,d)
class Seg:
def __init__(self, n):
self.n = 2**math.ceil(math.log2(n))
self.data = [unit]*(self.n*2-1)
def query(self, a, b):
def _query(a, b, k, l, r):
if b<=l or r<=a:
return unit
if a<=l and r<=b:
return self.data[k]
m = (l+r)//2
return mul(_query(a,b,k*2+1,l,m), _query(a,b,k*2+2,m,r))
return _query(a, b, 0, 0, self.n)
def update(self, a, v):
a += self.n-1
self.data[a] = v
while a > 0:
a = (a-1)//2
self.data[a] = mul(self.data[a*2+1], self.data[a*2+2])
n = int(input())
graph = [[] for _ in range(n)]
for i in range(n-1):
a, b = map(int, input().split())
graph[a].append((i,b))
graph[b].append((i,a))
edge = [0]*(n-1)
parent = [0]*n
bfs = [0]
for u in bfs:
for e, v in graph[u]:
if v == parent[u]:
continue
parent[v] = u
edge[e] = v
bfs.append(v)
size = [1]*n
heavy = [0]*n
for u in reversed(bfs):
for _, v in graph[u]:
if v != parent[u] and size[v]+1 > size[u]:
size[u] = size[v]+1
heavy[u] = v
chain = []
nxt = []
pos = [None]*n
for u in bfs:
if u == heavy[parent[u]]:
i, j = pos[parent[u]]
pos[u] = (i, j+1)
else:
pos[u] = (len(chain), 0)
nxt.append(parent[u])
chain.append(Seg(size[u]))
for _ in range(int(input())):
typ, *rest = input().split()
if typ == 'x':
i, *x = map(int, rest)
j, k = pos[edge[i]]
chain[j].update(k, tuple(x))
else:
i, j = map(int, rest)
a, b = pos[j]
c, d = pos[i]
res = unit
while a != c:
res = mul(chain[a].query(0, b+1), res)
a, b = pos[nxt[a]]
print(*mul(chain[a].query(d+1, b+1), res))
konsin_tokage