結果
問題 | No.650 行列木クエリ |
ユーザー | navel_tos |
提出日時 | 2024-02-12 16:37:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,635 ms / 2,000 ms |
コード長 | 8,080 bytes |
コンパイル時間 | 146 ms |
コンパイル使用メモリ | 82,260 KB |
実行使用メモリ | 241,848 KB |
最終ジャッジ日時 | 2024-04-27 20:59:36 |
合計ジャッジ時間 | 9,774 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
55,704 KB |
testcase_01 | AC | 1,038 ms
146,724 KB |
testcase_02 | AC | 1,601 ms
239,200 KB |
testcase_03 | AC | 38 ms
55,760 KB |
testcase_04 | AC | 1,346 ms
154,656 KB |
testcase_05 | AC | 1,599 ms
241,840 KB |
testcase_06 | AC | 38 ms
56,244 KB |
testcase_07 | AC | 38 ms
56,024 KB |
testcase_08 | AC | 951 ms
144,148 KB |
testcase_09 | AC | 1,635 ms
241,848 KB |
testcase_10 | AC | 37 ms
55,208 KB |
ソースコード
#yukicoder 650 行列木クエリ #Heavy-Light decomposition データ構造を使うならbuild必須なので注意 class HL_decomposition: class SegmentTree: #前提mod1 def __init__(self, n, identity_e, function): self._n = n; self._size = 1 << (n - 1).bit_length(); self._e = e = identity_e; self._f = function; self._node = [e] * 2 * self._size def build(self, A): for i,v in enumerate(A, start = self._size): self._node[i] = v for i in range(self._size - 1, 0, -1): self._node[i] = self._f(self._node[i<<1], self._node[i<<1|1]) def update(self, index, value): i = self._size + index; self._node[i] = value while i - 1: i >>= 1; self._node[i] = self._f(self._node[i<<1], self._node[i<<1|1]) def fold(self, Lt, Rt): Lt, Rt = Lt + self._size, Rt + self._size; vL = vR = self._e while Lt < Rt: if Lt & 1: vL = self._f(vL, self._node[Lt]); Lt += 1 if Rt & 1: Rt -= 1; vR = self._f(self._node[Rt], vR) Lt >>= 1; Rt >>= 1 return self._f(vL, vR) class SparseTable: #前提mod2 def __init__(self, n, identity_e, function): self._n = n; self._logn = (n - 1).bit_length(); self._size = 1 << self._logn; self._e = e = identity_e; self._f = function; self._T = [[e] * self._logn for _ in range(self._size)]; self._A = [e] * self._size def build(self, A): e, f, T = self._e, self._f, self._T; self._A = A = A + [e] * (self._size - self._n) for x in range(self._logn): t = 1 << x for s in range(t, self._size, t << 1): T[s][x] = A[s] for j in range(s + 1, s + t, +1): T[j][x] = f(T[j-1][x], A[j]) for s in range(self._size - t - 1, -1, - t << 1): T[s][x] = A[s] for j in range(s - 1, s - t, -1): T[j][x] = f(A[j], T[j+1][x]) def fold(self, Lt, Rt): Lt, Rt = max(0, Lt), min(self._size, Rt) - 1; x = (Lt ^ Rt).bit_length() - 1; return self._e if not 0 <= Lt <= Rt < self._size else self._A[Lt] if Lt == Rt else self._f( self._T[Lt][x], self._T[Rt][x] ) def __init__(self, N, G, identity_e = 0, func = 'add'): #pos[v] = i, order[i] = v 頂点vのDFS順序がi番目 #leader[i]: Heavy edgeの代表値のDFS順序 #depth[i]: 再帰の深さ parent[i]: ひとつ根側のDFS順序 #A[i]: u - vパスの重みw。DFS順序が遅い頂点v側に入れる。pos[u] < pos[v] = i self._N = N; self._logN = logN = N.bit_length(); self._e = e = identity_e self._f = f = (lambda x,y: x + y) if func == 'add' else func self._G = G = [[(v, e) for v in S] for S in G] if N > 1 and isinstance(G[0][0],int) else G self._A = A = [e for _ in range(N)]; self.pos = pos = [-1] * N self.order = order = [-1] * N; self.leader = leader = [-1] * N; size = [1] * N self.depth = depth = [-1] * N; self.parent = parent = [-1] * N; Q = [(0, -1)] for now,back in Q: #前処理 for nxt,_ in G[now]: if nxt != back: Q.append((nxt, now)) while Q: now,back = Q.pop(); size[back] += size[now] if back != -1 else 0 Q.append((0, -1, e, 0, -1)) #HL分解 for i in range(N): now, back, c, d, t = Q.pop(); pos[now], parent[i]= i, pos[back] order[i], A[i], depth[i] = now, c, d; leader[i] = t = t if t != -1 else i if size[now] > 1: #部分木のうち最大サイズのものを最後にappend s, v, x = 0, now, e for nxt,w in G[now]: if nxt == back: continue if s < size[nxt]: if s > 0: Q.append((v, now, x, d + 1, -1)) s, v, x = size[nxt], nxt, w else: Q.append((nxt, now, w, d + 1, -1)) Q.append((v, now, x, d, t)) def build(self, use_SegTree = True, A = 'no need to import'): N, e, f = self._N, self._e, self._f if A != 'no need to import': self._A = A self._ST = ST = self.SegmentTree if use_SegTree else self.SparseTable self._obvST = obvST = ST(N, e, f); obvST.build(self._A) self._revST = revST = ST(N, e, f); revST.build(self._A[::-1]) def LCA(self, u, v): #O(logN) i, j = self.pos[u], self.pos[v]; c, d = self.depth[i], self.depth[j] if c > d: i, j, c, d = j, i, c, d s, t = self.leader[i], self.leader[j] for d in range(d - 1, c - 1, -1): j = self.parent[t]; t = self.leader[j] while s != t: i, j = self.parent[s], self.parent[t]; s, t = self.leader[i], self.leader[j] return self.order[ min(i,j) ] def update(self, index_u, value_v, weight = None): if weight == None: j, w = self.pos[index_u], value_v else: i, j, w = self.pos[index_u], self.pos[value_v], weight; i, j = (j, i) if i > j else (i, j); assert self.parent[j] == i, 'not connect Tree edge' self._A[j] = w; self._obvST.update(j, w); self._revST.update(self._N - 1 - j, w) def fold(self, u, v, path_query = False): #u→vパスの作用値を取得 Lt = Rt = self._e; f = self._f; i, j = self.pos[u], self.pos[v] c, d = self.depth[i], self.depth[j]; s, t = self.leader[i], self.leader[j] for c in range(c - 1, d - 1, -1): Lt = f( Lt, self._revST.fold(N - 1 - i, N - s) ); i = self.parent[s]; s = self.leader[i] for d in range(d - 1, c - 1, -1): Rt = f( self._obvST.fold(t, j + 1), Rt ); j = self.parent[t]; t = self.leader[j] while s != t: Lt, Rt = f( Lt, self._revST.fold(N-1-i,N-s) ), f( self._obvST.fold(t,j+1), Rt ); i, j = self.parent[s], self.parent[t]; s, t = self.leader[i], self.leader[j] if i > j: LCA, Lt = j, f( Lt, self._revST.fold(N - i - 1, N - j - 1) ) elif i < j: LCA, Rt = i, f( self._obvST.fold(i + 1, j + 1), Rt ) else: LCA = i LCA = self._e if path_query else self._A[LCA]; return f( f(Lt, LCA), Rt ) #行列累乗 1行N列の行列は[[1, 2, ...]] と2重括弧に自動変換するので注意 class matrix_pow: def __init__(self,MOD=998244353): self._MOD=MOD def eye(self,N): #単位行列の作成 return [[1 if i==j else 0 for j in range(N)] for i in range(N)] def add(self,A,B): #行列の加算 if isinstance(A[0],int): A=[A] if isinstance(B[0],int): B=[B] assert len(A) ==len(B), 'not same size' assert len(A[0])==len(B[0]), 'not same size' nG=[[0]*max(len(A[i]) for i in range(len(A))) for _ in range(len(A))] for h in range(len(nG)): for w in range(len(nG[h])): if len(A[h])<w: nG[h][w]+=A[h][w] if len(B[h])<w: nG[h][w]+=B[h][w] nG[h][w]%=self._MOD return nG def mul(self,A,B): #行列積 L行M列 * M行N列 = L行N列 if isinstance(A[0],int): A=[A] if isinstance(B[0],int): B=[B] assert len(A[0])==len(B), 'cannot calcurate' nG=[[0]*max(len(B[i]) for i in range(len(B))) for _ in range(len(A))] for h in range(len(nG)): for w in range(len(nG[0])): for x in range(len(A[0])): nG[h][w]+=A[h][x]*B[x][w]%self._MOD; nG[h][w]%=self._MOD return nG #入力高速化したらこれでも通るやろ import sys input = sys.stdin.readline #入力受取 N = int(input()) G = [[] for _ in range(N)] H = [] for _ in range(N - 1): a,b = map(int,input().split()) G[a].append(b) G[b].append(a) H.append((a,b)) #HLDに乗せる 初期値は単位行列 mp = matrix_pow(10 ** 9 + 7) HLD = HL_decomposition(N, G, [[1,0], [0,1]], mp.mul) HLD.build(True) #クエリを処理 for _ in range(int(input())): t = list(input().split()) if t[0] == 'x': i,w,x,y,z = map(int, t[1:]) u,v = H[i] HLD.update(u, v, [[w,x],[y,z]]) if t[0] == 'g': i,j = map(int, t[1:]) ((w,x),(y,z)) = HLD.fold(i, j, True) print(w,x,y,z)