結果

問題 No.650 行列木クエリ
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-03-06 22:22:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 899 ms / 2,000 ms
コード長 2,076 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 81,688 KB
実行使用メモリ 118,756 KB
最終ジャッジ日時 2023-10-21 20:43:24
合計ジャッジ時間 5,178 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,888 KB
testcase_01 AC 537 ms
89,896 KB
testcase_02 AC 774 ms
116,740 KB
testcase_03 AC 39 ms
53,768 KB
testcase_04 AC 597 ms
92,596 KB
testcase_05 AC 899 ms
118,756 KB
testcase_06 AC 39 ms
53,892 KB
testcase_07 AC 38 ms
53,892 KB
testcase_08 AC 398 ms
91,944 KB
testcase_09 AC 528 ms
115,276 KB
testcase_10 AC 38 ms
53,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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))
0