結果

問題 No.650 行列木クエリ
ユーザー tamatotamato
提出日時 2020-05-04 16:31:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,390 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 146,304 KB
最終ジャッジ日時 2024-06-24 13:26:50
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,040 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 43 ms
54,784 KB
testcase_07 AC 44 ms
54,784 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 43 ms
54,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline
    from collections import deque

    def matmul(A, B):
        C = [[0] * len(B[0]) for _ in range(len(A))]
        for i in range(len(A)):
            for k in range(len(B)):
                for j in range(len(B[0])):
                    C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod
        return C

    class SegmentTree:
        def __init__(self, A, initialize=True, segfunc=min, ident=2000000000):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.segfunc = segfunc
            self.ident = ident
            if initialize:
                self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.ident] * (self.N0 * 2)

        def update(self, i, x):
            i += self.N0 - 1
            self.data[i] = x
            for _ in range(self.LV):
                i >>= 1
                self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            ret = self.ident
            while l < r:
                if l & 1:
                    ret = self.segfunc(self.data[l], ret)
                    l += 1
                if r & 1:
                    ret = self.segfunc(ret, self.data[r - 1])
                    r -= 1
                l >>= 1
                r >>= 1
            return ret

    N = int(input())
    adj = [[] for _ in range(N + 1)]
    E = [[] for _ in range(N-1)]
    for e in range(N - 1):
        a, b = map(int, input().split())
        a += 1
        b += 1
        adj[a].append(b)
        adj[b].append(a)
        E[e] = (a, b)

    que = deque()
    que.append(1)
    depth = [-1] * (N + 1)
    depth[1] = 0
    par = [0] * (N + 1)
    child = [[] for _ in range(N + 1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if depth[u] == -1:
                depth[u] = depth[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()
    size = [1] * (N + 1)
    largest_child = [0] * (N + 1)
    for v in seq:
        child_size_max = 0
        for u in child[v]:
            size[v] += size[u]
            if size[u] > child_size_max:
                child_size_max = size[u]
                largest_child[v] = u

    seq.reverse()
    idx_of_array = [0] * (N + 1)
    idx_in_array = [0] * (N + 1)
    seen = [0] * (N + 1)
    compressed_tree = []
    for v in seq:
        if seen[v]:
            continue
        seen[v] = 1
        compressed_tree.append([v])
        idx_of_array[v] = len(compressed_tree) - 1
        idx_in_array[v] = 0
        p = v
        cnt = 0
        while largest_child[p]:
            cnt += 1
            u = largest_child[p]
            seen[u] = 1
            compressed_tree[-1].append(u)
            idx_of_array[u] = len(compressed_tree) - 1
            idx_in_array[u] = cnt
            p = u

    ST = []
    for i in range(len(compressed_tree)):
        ST.append(SegmentTree(compressed_tree[i], initialize=False, segfunc=matmul, ident=[[1, 0], [0, 1]]))

    for _ in range(int(input())):
        q = list(input().split())
        if q[0] == "x":
            q = q[1:]
            e, a, b, c, d = map(int, q)
            v1, v2 = E[e]
            if depth[v1] < depth[v2]:
                v = v2
            else:
                v = v1
            ST[idx_of_array[v]].update(idx_in_array[v] + 1, [[a, b], [c, d]])
        else:
            q = q[1:]
            a, d = map(int, q)
            a += 1
            d += 1
            ans = [[1, 0], [0, 1]]
            while idx_of_array[a] != idx_of_array[d]:
                ans = matmul(ST[idx_of_array[d]].query(1, idx_in_array[d] + 2), ans)
                d = par[compressed_tree[idx_of_array[d]][0]]
            if a != d:
                ans = matmul(ST[idx_of_array[a]].query(idx_in_array[a] + 2, idx_in_array[d] + 2), ans)
            print(ans[0][0], ans[0][1], ans[1][0], ans[1][1])


if __name__ == '__main__':
    main()
0