結果
問題 | No.235 めぐるはめぐる (5) |
ユーザー |
|
提出日時 | 2020-09-12 18:45:00 |
言語 | Nim (2.2.0) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 8,818 bytes |
コンパイル時間 | 1,163 ms |
コンパイル使用メモリ | 74,112 KB |
最終ジャッジ日時 | 2025-01-02 12:10:26 |
合計ジャッジ時間 | 2,024 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
/home/judge/data/code/Main.nim(233, 39) Error: tuple expected for tuple unpacking, but got 'seq[int]'
ソースコード
import algorithm, deques, heapqueue, math, sets, sequtils, strutils, sugar, tablesproc input*(): string =return stdin.readLineproc chmax*[T: SomeNumber](num0: var T, num1: T) =num0 = max(num0, num1)proc chmin*[T: SomeNumber](num0: var T, num1: T) =num0 = min(num0, num1)proc `%=`*[T: SomeInteger](num0: var T, num1: T) =num0 = num0 mod num1const MOD = 10^9 + 7typeHeavyLightDecomposition* = ref objectgraph: seq[seq[Natural]]path_root, path_parent, left, right: seq[Natural]proc initHeavyLightdecomposition*(size: Positive): HeavyLightDecomposition =vargraph = newSeqWith(size, newSeq[Natural]())empty_seq = newSeq[Natural]()return HeavyLightDecomposition(graph: graph, path_root: empty_seq, path_parent: empty_seq, left: empty_seq, right: empty_seq)proc add_edge*(self: var HeavyLightDecomposition, a, b: Natural) =self.graph[a].add(b)self.graph[b].add(a)proc build*(self: var HeavyLightDecomposition, root: Natural) =varstack = @[(root, root)]q = newSeq[Natural]()v, p: Naturalwhile stack.len != 0:(v, p) = stack.pop()q.add(v)for i, to in self.graph[v]:if to == p:self.graph[v][i] = self.graph[v][^1]let _ = self.graph[v].pop()breakfor to in self.graph[v]:stack.add((to, v))let n = self.graph.lenvar size = newSeqWith(n, 1)for v in reversed(q):for i, to in self.graph[v]:size[v] += size[to]if size[self.graph[v][0]] < size[to]:(self.graph[v][0], self.graph[v][i]) = (self.graph[v][i], self.graph[v][0])self.path_root = newSeqWith(n, root)self.path_parent = newSeqWith(n, root)self.left = newSeq[Natural](n)self.right = newSeq[Natural](n)vark = 0stack1 = @[(root, 0)]op: intto: Naturalwhile stack1.len != 0:(v, op) = stack1.pop()if op == 1:self.right[v] = kcontinueself.left[v] = kinc kstack1.add((v, 1))if 1 < self.graph[v].len:for i, to in self.graph[v][1..^1]:self.path_root[to] = toself.path_parent[to] = vstack1.add((to, 0))if self.graph[v].len != 0:to = self.graph[v][0]self.path_root[to] = self.path_root[v]self.path_parent[to] = self.path_parent[v]stack1.add((to, 0))proc sub_tree*(self: var HeavyLightDecomposition, v: Natural): (Natural, Natural) =return (self.left[v], self.right[v])proc path*(self: var HeavyLightDecomposition, v, u: Natural): seq[(Natural, int)] =varx = vy = ures = newSeq[(Natural, int)]()p: Naturalwhile self.path_root[x] != self.path_root[y]:if self.left[x] < self.left[y]:p = self.path_root[y]res.add((self.left[p], self.left[y] + 1))y = self.path_parent[y]else:p = self.path_root[x]res.add((self.left[p], self.left[x] + 1))x = self.path_parent[x]res.add((min(self.left[x], self.left[y]), max(self.left[x], self.left[y]) + 1))return resproc bit_length(n: Natural): Natural =const BIT_SIZE = 24if n == 0:return 0let s = toBin(n, BIT_SIZE)return BIT_SIZE - s.find('1')typeLazySegmentTree*[T, K] = ref objectLV: NaturalN0: Positiveide_ele: Tlazy_ide_ele: Kdata: seq[T]lazy_data: seq[K]fold: proc (a, b: T): Teval: proc (a: T, b: K): Tmerge: proc (a, b: K): Kproc initLazySegmentTree*[T, K](size: Positive, ide_ele: T, lazy_ide_ele: K, fold: proc (a, b: T): T, eval: proc (a: T, b: K): T, merge: proc (a, b:K): K): LazySegmentTree[T, K] =varLV = bit_length(size - 1)N0 = 1 shl LVdata = newSeqWith(2*N0, ide_ele)lazy_data = newSeqWith(2*N0, lazy_ide_ele)return LazySegmentTree[T, K](LV: LV, N0: N0, ide_ele: ide_ele, lazy_ide_ele: lazy_ide_ele, data: data, lazy_data: lazy_data, fold: fold, eval:eval, merge: merge)proc toLazySegmentTree*[T, K](init_value: openArray[T], ide_ele: T, lazy_ide_ele: K, fold: proc (a, b: T): T, eval: proc (a: T, b: K): T, merge: proc(a, b: K): K): LazySegmentTree[T, K] =varLV = bit_length(init_value.len - 1)N0 = 1 shl LVdata = newSeqWith(2*N0, ide_ele)lazy_data = newSeqWith(2*N0, lazy_ide_ele)for i, x in init_value:data[i + N0 - 1] = xfor i in countdown(N0 - 2, 0):data[i] = fold(data[2*i + 1], data[2*i + 2])return LazySegmentTree[T, K](LV: LV, N0: N0, ide_ele: ide_ele, lazy_ide_ele: lazy_ide_ele, data: data, lazy_data: lazy_data, fold: fold, eval:eval, merge: merge)iterator gindex*[T, K](self: var LazySegmentTree[T, K], left, right: Natural): Natural =varL = (left + self.N0) shr 1R = (right + self.N0) shr 1lc = if (left and 1) == 1: 0 else: bit_length(L and -L)rc = if (right and 1) == 1: 0 else: bit_length(R and -R)for i in 0..<self.LV:if rc <= i:yield Rif L < R and lc <= i:yield LL = L shr 1R = R shr 1proc propagates*[T, K](self: var LazySegmentTree[T, K], ids: seq[Natural]) =varidx: Naturalv: Kfor id in reversed(ids):idx = id - 1v = self.lazy_data[idx]if v == self.lazy_ide_ele:continue# v = v shr 1self.data[2*idx + 1] = self.eval(self.data[2*idx + 1], v)self.data[2*idx + 2] = self.eval(self.data[2*idx + 2], v)self.lazy_data[2*idx + 1] = self.merge(self.lazy_data[2*idx + 1], v)self.lazy_data[2*idx + 2] = self.merge(self.lazy_data[2*idx + 2], v)self.lazy_data[idx] = self.lazy_ide_eleproc update*[T, K](self: var LazySegmentTree[T, K], left, right: Natural, x: K) =let ids = toSeq(self.gindex(left, right))# self.propagates(ids)varL = left + self.N0R = right + self.N0# x = xwhile L < R:if (L and 1) == 1:self.lazy_data[L - 1] = self.merge(self.lazy_data[L - 1], x)self.data[L - 1] = self.eval(self.data[L - 1], x)inc Lif (R and 1) == 1:dec Rself.lazy_data[R - 1] = self.merge(self.lazy_data[R - 1], x)self.data[R - 1] = self.eval(self.data[R - 1], x)L = L shr 1R = R shr 1# x = x shl 1var idx: Naturalfor id in ids:idx = id - 1self.data[idx] = self.eval(self.fold(self.data[2*idx + 1], self.data[2*idx + 2]), self.lazy_data[idx])proc query*[T, K](self: var LazySegmentTree[T, K], left, right: Natural): T =self.propagates(toSeq(self.gindex(left, right)))varL = left + self.N0R = right + self.N0res = self.ide_elewhile L < R:if (L and 1) == 1:res = self.fold(res, self.data[L - 1])inc Lif (R and 1) == 1:dec Rres = self.fold(res, self.data[R - 1])L = L shr 1R = R shr 1return resvarS, C, ans: seq[int]init_val: seq[(int, int)]hld: HeavyLightDecompositionlazy_seg_tree: LazySegmentTree[(int, int), int]proc solve() =let N = input().parseIntS = input().split.map(parseInt)C = input().split.map(parseInt)hld = initHeavyLightdecomposition(N)var ai, bi: intfor _ in 0..<N - 1:(ai, bi) = input().split.mapIt(it.parseInt - 1)hld.add_edge(ai, bi)hld.build(0)init_val = newSeqWith(N, (0, 0))for i, (si, ci) in zip(S, C):let idx = hld.sub_tree(i)[0]init_val[idx] = (si, ci)lazy_seg_tree = toLazySegmentTree(init_val, (0, 0), 0, (a, b) => ((a[0] + b[0]) mod MOD, (a[1] + b[1]) mod MOD),(a, b) => ((a[0] + a[1]*b) mod MOD, a[1]), (a, b) => (a + b) mod MOD)let Q = input().parseIntvar xi, yi, zi, cost: intfor _ in 0..<Q:let inputs = input().split.map(parseInt)if inputs[0] == 0:(xi, yi, zi) = inputs[1..^1]dec xi; dec yifor (left, right) in hld.path(xi, yi):lazy_seg_tree.update(left, right, zi)else:(xi, yi) = inputs[1..^1]dec xi; dec yicost = 0for (left, right) in hld.path(xi, yi):cost += lazy_seg_tree.query(left, right)[0]cost %= MODans.add(cost)echo ans.join("\n")when is_main_module:solve()