結果
問題 | No.1054 Union add query |
ユーザー | kemuniku |
提出日時 | 2024-09-16 02:39:14 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 889 ms / 2,000 ms |
コード長 | 30,993 bytes |
コンパイル時間 | 4,585 ms |
コンパイル使用メモリ | 81,536 KB |
実行使用メモリ | 122,624 KB |
最終ジャッジ日時 | 2024-09-16 02:39:25 |
合計ジャッジ時間 | 10,967 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 544 ms
61,312 KB |
testcase_04 | AC | 889 ms
122,624 KB |
testcase_05 | AC | 467 ms
53,632 KB |
testcase_06 | AC | 445 ms
89,728 KB |
testcase_07 | AC | 392 ms
89,728 KB |
testcase_08 | AC | 469 ms
89,856 KB |
testcase_09 | AC | 753 ms
95,744 KB |
testcase_10 | AC | 508 ms
95,616 KB |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) # source: src/cplib/tree/heavylightdecomposition.nim ImportExpand "cplib/tree/heavylightdecomposition" <=== "when not declared CPLIB_TREE_HLD:\n const CPLIB_TREE_HLD* = 1\n import sequtils\n import algorithm\n import sets\n when not declared CPLIB_GRAPH_GRAPH:\n const CPLIB_GRAPH_GRAPH* = 1\n \n import sequtils\n import math\n type DynamicGraph*[T] = ref object of RootObj\n edges*: seq[seq[(int32, T)]]\n len*: int\n type StaticGraph*[T] = ref object of RootObj\n src*, dst*: seq[int32]\n cost*: seq[T]\n elist*: seq[(int32, T)]\n start*: seq[int32]\n len*: int\n \n type WeightedDirectedGraph*[T] = ref object of DynamicGraph[T]\n type WeightedUnDirectedGraph*[T] = ref object of DynamicGraph[T]\n type UnWeightedDirectedGraph* = ref object of DynamicGraph[int]\n type UnWeightedUnDirectedGraph* = ref object of DynamicGraph[int]\n type WeightedDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type WeightedUnDirectedStaticGraph*[T] = ref object of StaticGraph[T]\n type UnWeightedDirectedStaticGraph* = ref object of StaticGraph[int]\n type UnWeightedUnDirectedStaticGraph* = ref object of StaticGraph[int]\n \n type GraphTypes*[T] = DynamicGraph[T] or StaticGraph[T]\n type DirectedGraph* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph\n type UnDirectedGraph* = WeightedUnDirectedGraph or UnWeightedUnDirectedGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type WeightedGraph*[T] = WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T] or WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T]\n type UnWeightedGraph* = UnWeightedDirectedGraph or UnWeightedUnDirectedGraph or UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n type DynamicGraphTypes* = WeightedDirectedGraph or UnWeightedDirectedGraph or WeightedUnDirectedGraph or UnWeightedUnDirectedGraph\n type StaticGraphTypes* = WeightedDirectedStaticGraph or UnWeightedDirectedStaticGraph or WeightedUnDirectedStaticGraph or UnWeightedUnDirectedStaticGraph\n \n proc add_edge_dynamic_impl*[T](g: DynamicGraph[T], u, v: int, cost: T, directed: bool) =\n g.edges[u].add((v.int32, cost))\n if not directed: g.edges[v].add((u.int32, cost))\n \n proc initWeightedDirectedGraph*(N: int, edgetype: typedesc = int): WeightedDirectedGraph[edgetype] =\n result = WeightedDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, true)\n \n proc initWeightedUnDirectedGraph*(N: int, edgetype: typedesc = int): WeightedUnDirectedGraph[edgetype] =\n result = WeightedUnDirectedGraph[edgetype](edges: newSeq[seq[(int32, edgetype)]](N), len: N)\n proc add_edge*[T](g: var WeightedUnDirectedGraph[T], u, v: int, cost: T) =\n g.add_edge_dynamic_impl(u, v, cost, false)\n \n proc initUnWeightedDirectedGraph*(N: int): UnWeightedDirectedGraph =\n result = UnWeightedDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, true)\n \n proc initUnWeightedUnDirectedGraph*(N: int): UnWeightedUnDirectedGraph =\n result = UnWeightedUnDirectedGraph(edges: newSeq[seq[(int32, int)]](N), len: N)\n proc add_edge*(g: var UnWeightedUnDirectedGraph, u, v: int) =\n g.add_edge_dynamic_impl(u, v, 1, false)\n \n proc len*[T](G: WeightedGraph[T]): int = G.len\n proc len*(G: UnWeightedGraph): int = G.len\n \n iterator `[]`*[T](g: WeightedDirectedGraph[T] or WeightedUnDirectedGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator `[]`*(g: UnWeightedDirectedGraph or UnWeightedUnDirectedGraph, x: int): int =\n for e in g.edges[x]: yield e[0].int\n \n proc add_edge_static_impl*[T](g: StaticGraph[T], u, v: int, cost: T, directed: bool) =\n g.src.add(u.int32)\n g.dst.add(v.int32)\n g.cost.add(cost)\n if not directed:\n g.src.add(v.int32)\n g.dst.add(u.int32)\n g.cost.add(cost)\n \n proc build_impl*[T](g: StaticGraph[T]) =\n g.start = newSeqWith(g.len + 1, 0.int32)\n for i in 0..<g.src.len:\n g.start[g.src[i]] += 1\n g.start.cumsum\n g.elist = newSeq[(int32, T)](g.start[^1])\n for i in countdown(g.src.len - 1, 0):\n var u = g.src[i]\n var v = g.dst[i]\n g.start[u] -= 1\n g.elist[g.start[u]] = (v, g.cost[i])\n proc build*(g: StaticGraphTypes) = g.build_impl()\n \n proc initWeightedDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedDirectedStaticGraph[edgetype] =\n result = WeightedDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[edgetype](capacity),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, true)\n \n proc initWeightedUnDirectedStaticGraph*(N: int, edgetype: typedesc = int, capacity: int = 0): WeightedUnDirectedStaticGraph[edgetype] =\n result = WeightedUnDirectedStaticGraph[edgetype](\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[edgetype](capacity*2),\n elist: newSeq[(int32, edgetype)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*[T](g: var WeightedUnDirectedStaticGraph[T], u, v: int, cost: T) =\n g.add_edge_static_impl(u, v, cost, false)\n \n proc initUnWeightedDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedDirectedStaticGraph =\n result = UnWeightedDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity),\n dst: newSeqOfCap[int32](capacity),\n cost: newSeqOfCap[int](capacity),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, true)\n \n proc initUnWeightedUnDirectedStaticGraph*(N: int, capacity: int = 0): UnWeightedUnDirectedStaticGraph =\n result = UnWeightedUnDirectedStaticGraph(\n src: newSeqOfCap[int32](capacity*2),\n dst: newSeqOfCap[int32](capacity*2),\n cost: newSeqOfCap[int](capacity*2),\n elist: newSeq[(int32, int)](0),\n start: newSeq[int32](0),\n len: N\n )\n proc add_edge*(g: var UnWeightedUnDirectedStaticGraph, u, v: int) =\n g.add_edge_static_impl(u, v, 1, false)\n \n proc static_graph_initialized_check*[T](g: StaticGraph[T]) = assert g.start.len > 0, \"Static Graph must be initialized before use.\"\n \n iterator `[]`*[T](g: WeightedDirectedStaticGraph[T] or WeightedUnDirectedStaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n iterator `[]`*(g: UnWeightedDirectedStaticGraph or UnWeightedUnDirectedStaticGraph, x: int): int =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield g.elist[i][0].int\n \n iterator to_and_cost*[T](g: DynamicGraph[T], x: int): (int, T) =\n for e in g.edges[x]: yield (e[0].int, e[1])\n iterator to_and_cost*[T](g: StaticGraph[T], x: int): (int, T) =\n g.static_graph_initialized_check()\n for i in g.start[x]..<g.start[x+1]: yield (g.elist[i][0].int, g.elist[i][1])\n \n # https://atcoder.jp/contests/abc337/submissions/50216964\n # ↑上記の提出より引用\n type HeavyLightDecomposition* = object\n N*: int\n P*, PP*, PD*, D*, I*, rangeL*, rangeR*: seq[int]\n proc initHld*(g: UnDirectedGraph, root: int): HeavyLightDecomposition =\n var hld = HeavyLightDecomposition()\n var n: int = g.len\n hld.N = n\n hld.P = newSeqWith(n, -1)\n hld.I = newSeqWith(n, 0)\n hld.I[0] = root\n var iI = 1\n for i in 0..<n:\n var p = hld.I[i]\n for e in g[p]:\n if hld.P[p] != e:\n hld.I[iI] = e\n hld.P[e] = p\n iI += 1\n var Z = newSeqWith(n, 1)\n var nx = newSeqWith(n, -1)\n hld.PP = newSeqWith(n, 0)\n for i in 0..<n:\n hld.PP[i] = i\n for i in 1..<n:\n var p = hld.I[n-i]\n Z[hld.P[p]] += Z[p]\n if nx[hld.P[p]] == -1 or Z[nx[hld.P[p]]] < Z[p]:\n nx[hld.P[p]] = p\n for p in hld.I:\n if nx[p] != -1:\n hld.PP[nx[p]] = p\n hld.PD = newSeqWith(n, n)\n hld.PD[root] = 0\n hld.D = newSeqWith(n, 0)\n for p in hld.I:\n if p != root:\n hld.PP[p] = hld.PP[hld.PP[p]]\n hld.PD[p] = min(hld.PD[hld.PP[p]], hld.PD[hld.P[p]]+1)\n hld.D[p] = hld.D[hld.P[p]]+1\n hld.rangeL = newSeqWith(n, 0)\n hld.rangeR = newSeqWith(n, 0)\n for p in hld.I:\n hld.rangeR[p] = hld.rangeL[p] + Z[p]\n var ir = hld.rangeR[p]\n for e in g[p]:\n if hld.P[p] != e and e != nx[p]:\n ir -= Z[e]\n hld.rangeL[e] = ir\n if nx[p] != -1:\n hld.rangeL[nx[p]] = hld.rangeL[p] + 1\n for i in 0..<n:\n hld.I[hld.rangeL[i]] = i\n return hld\n proc initHld*(g: DirectedGraph, root: int): HeavyLightDecomposition =\n var n = g.len\n var gn = initUnWeightedUnDirectedStaticGraph(n)\n var seen = initHashSet[(int, int)]()\n for i in 0..<n:\n for (j, _) in g[i]:\n if (i, j) notin seen:\n gn.add_edge(i, j)\n seen.incl((i, j))\n seen.incl((j, i))\n gn.build\n return initHld(gn, root)\n proc initHld*(adj: seq[seq[int]], root: int): HeavyLightDecomposition =\n var n = adj.len\n var gn = initUnWeightedUnDirectedStaticGraph(n)\n var seen = initHashSet[(int, int)]()\n for i in 0..<n:\n for j in adj[i]:\n if (i, j) notin seen:\n gn.add_edge(i, j)\n seen.incl((i, j))\n seen.incl((j, i))\n gn.build\n return initHld(gn, root)\n proc numVertices*(hld: HeavyLightDecomposition): int = hld.N\n proc depth*(hld: HeavyLightDecomposition, p: int): int = hld.D[p]\n proc toSeq*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeL[vtx]\n proc toVtx*(hld: HeavyLightDecomposition, seqidx: int): int = hld.I[seqidx]\n proc toSeq2In*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeL[vtx] * 2 - hld.D[vtx]\n proc toSeq2Out*(hld: HeavyLightDecomposition, vtx: int): int = hld.rangeR[vtx] * 2 - hld.D[vtx] - 1\n proc parentOf*(hld: HeavyLightDecomposition, v: int): int = hld.P[v]\n proc heavyRootOf*(hld: HeavyLightDecomposition, v: int): int = hld.PP[v]\n proc heavyChildOf*(hld: HeavyLightDecomposition, v: int): int =\n if hld.toSeq(v) == hld.N-1:\n return -1\n var cand = hld.toVtx(hld.toSeq(v) + 1)\n if hld.PP[v] == hld.PP[cand]:\n return cand\n -1\n proc lca*(hld: HeavyLightDecomposition, u: int, v: int): int =\n var (u, v) = (u, v)\n if hld.PD[u] < hld.PD[v]:\n swap(u, v)\n while hld.PD[u] > hld.PD[v]:\n u = hld.P[hld.PP[u]]\n while hld.PP[u] != hld.PP[v]:\n u = hld.P[hld.PP[u]]\n v = hld.P[hld.PP[v]]\n if hld.D[u] > hld.D[v]:\n return v\n u\n proc dist*(hld: HeavyLightDecomposition, u: int, v: int): int =\n hld.depth(u) + hld.depth(v) - hld.depth(hld.lca(u, v)) * 2\n proc path*(hld: HeavyLightDecomposition, r: int, c: int, include_root: bool, reverse_path: bool): seq[(int, int)] =\n var (r, c) = (r, c)\n var k = hld.PD[c] - hld.PD[r] + 1\n if k <= 0:\n return @[]\n var res = newSeqWith(k, (0, 0))\n for i in 0..<k-1:\n res[i] = (hld.rangeL[hld.PP[c]], hld.rangeL[c] + 1)\n c = hld.P[hld.PP[c]]\n if hld.PP[r] != hld.PP[c] or hld.D[r] > hld.D[c]:\n return @[]\n var root_off = int(not include_root)\n res[^1] = (hld.rangeL[r]+root_off, hld.rangeL[c]+1)\n if res[^1][0] == res[^1][1]:\n discard res.pop()\n k -= 1\n if reverse_path:\n for i in 0..<k:\n res[i] = (hld.N - res[i][1], hld.N - res[i][0])\n else:\n res.reverse()\n res\n proc subtree*(hld: HeavyLightDecomposition, p: int): (int, int) = (hld.rangeL[p], hld.rangeR[p])\n proc median*(hld: HeavyLightDecomposition, x: int, y: int, z: int): int =\n hld.lca(x, y) xor hld.lca(y, z) xor hld.lca(x, z)\n proc la*(hld: HeavyLightDecomposition, starting: int, goal: int, d: int): int =\n var (u, v, d) = (starting, goal, d)\n if d < 0:\n return -1\n var g = hld.lca(u, v)\n var dist0 = hld.D[u] - hld.D[g] * 2 + hld.D[v]\n if dist0 < d:\n return -1\n var p = u\n if hld.D[u] - hld.D[g] < d:\n p = v\n d = dist0 - d\n while hld.D[p] - hld.D[hld.PP[p]] < d:\n d -= hld.D[p] - hld.D[hld.PP[p]] + 1\n p = hld.P[hld.PP[p]]\n hld.I[hld.rangeL[p] - d]\n iterator children*(hld: HeavyLightDecomposition, v: int): int =\n var s = hld.rangeL[v] + 1\n while s < hld.rangeR[v]:\n var w = hld.toVtx(s)\n yield w\n s += hld.rangeR[w] - hld.rangeL[w]\n" # source: src/cplib/collections/unionfind.nim ImportExpand "cplib/collections/unionfind" <=== "when not declared CPLIB_COLLECTIONS_UNIONFIND:\n const CPLIB_COLLECTIONS_UNIONFIND* = 1\n import algorithm\n import sequtils\n type UnionFind* = ref object\n count*: int\n par_or_siz: seq[int]\n proc initUnionFind*(N: int): UnionFind =\n result = UnionFind(count: N, par_or_siz: newSeqwith(N, -1))\n proc root*(self: UnionFind, x: int): int =\n if self.par_or_siz[x] < 0:\n return x\n else:\n self.par_or_siz[x] = self.root(self.par_or_siz[x])\n return self.par_or_siz[x]\n proc issame*(self: UnionFind, x: int, y: int): bool =\n return self.root(x) == self.root(y)\n proc unite*(self: UnionFind, x: int, y: int) =\n var x = self.root(x)\n var y = self.root(y)\n if(x != y):\n if(self.par_or_siz[x] > self.par_or_siz[y]):\n swap(x, y)\n self.par_or_siz[x] += self.par_or_siz[y]\n self.par_or_siz[y] = x\n self.count -= 1\n proc siz*(self: UnionFind, x: int): int =\n var x = self.root(x)\n return -self.par_or_siz[x]\n" # source: src/cplib/tmpl/sheep.nim ImportExpand "cplib/tmpl/sheep" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n import std/lenientops\n import options\n #入力系\n proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n proc lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\n proc si(): string {.inline.} =\n result = \"\"\n var c: char\n while true:\n c = getchar()\n if c == ' ' or c == '\\n':\n break\n result &= c\n #chmin,chmax\n template `max=`(x, y) = x = max(x, y)\n template `min=`(x, y) = x = min(x, y)\n #bit演算\n proc `%`*(x: int, y: int): int =\n result = x mod y\n if y > 0 and result < 0: result += y\n if y < 0 and result > 0: result += y\n proc `//`*(x: int, y: int): int{.inline.} =\n result = x div y\n if y > 0 and result * y > x: result -= 1\n if y < 0 and result * y < x: result -= 1\n proc `%=`(x: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `**=`(x: var int, y: int): void = x = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `~`(x: int): int = not x\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\n when not declared CPLIB_UTILS_CONSTANTS:\n const CPLIB_UTILS_CONSTANTS* = 1\n const INF32*: int32 = 100100111.int32\n const INF64*: int = int(3300300300300300491)\n \n const INF = INF64\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n\n #joinが非stringでめちゃくちゃ遅いやつのパッチ\n proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n" # source: src/atcoder/lazysegtree.nim ImportExpand "atcoder/lazysegtree" <=== "when not declared ATCODER_LAZYSEGTREE_HPP:\n const ATCODER_LAZYSEGTREE_HPP* = 1\n \n when not declared ATCODER_INTERNAL_BITOP_HPP:\n const ATCODER_INTERNAL_BITOP_HPP* = 1\n import std/bitops\n \n #ifdef _MSC_VER\n #include <intrin.h>\n #endif\n \n # @param n `0 <= n`\n # @return minimum non-negative `x` s.t. `n <= 2**x`\n proc ceil_pow2*(n:SomeInteger):int =\n var x = 0\n while (1.uint shl x) < n.uint: x.inc\n return x\n # @param n `1 <= n`\n # @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`\n proc bsf*(n:SomeInteger):int =\n return countTrailingZeroBits(n)\n \n when not declared ATCODER_RANGEUTILS_HPP:\n const ATCODER_RANGEUTILS_HPP* = 1\n type RangeType* = Slice[int] | HSlice[int, BackwardsIndex] | Slice[BackwardsIndex]\n type IndexType* = int | BackwardsIndex\n template halfOpenEndpoints*(p:Slice[int]):(int,int) = (p.a, p.b + 1)\n template `^^`*(s, i: untyped): untyped =\n (when i is BackwardsIndex: s.len - int(i) else: int(i))\n template halfOpenEndpoints*[T](s:T, p:RangeType):(int,int) =\n (s^^p.a, s^^p.b + 1)\n \n import std/sequtils\n import std/algorithm\n {.push inline.}\n type LazySegTree*[S,F;p:static[tuple]] = object\n len*, size*, log*:int\n d*:seq[S]\n lz*:seq[F]\n\n template calc_op*[ST:LazySegTree](self:ST or typedesc[ST], a, b:ST.S):auto =\n block:\n let u = ST.p.op(a, b)\n u\n template calc_e*[ST:LazySegTree](self:ST or typedesc[ST]):auto =\n block:\n let u = ST.p.e()\n u\n template calc_mapping*[ST:LazySegTree](self:ST or typedesc[ST], a:ST.F, b:ST.S):auto =\n block:\n let u = ST.p.mapping(a, b)\n u\n template calc_composition*[ST:LazySegTree](self:ST or typedesc[ST], a, b:ST.F):auto =\n block:\n # こう書かないとバグる事象を検出\n let u = ST.p.composition(a, b)\n u\n template calc_id*[ST:LazySegTree](self:ST or typedesc[ST]):auto =\n block:\n let u = ST.p.id()\n u\n\n proc update[ST:LazySegTree](self:var ST, k:int) =\n self.d[k] = ST.calc_op(self.d[2 * k], self.d[2 * k + 1])\n proc all_apply*[ST:LazySegTree](self:var ST, k:int, f:ST.F) =\n self.d[k] = ST.calc_mapping(f, self.d[k])\n if k < self.size:\n self.lz[k] = ST.calc_composition(f, self.lz[k])\n proc all_apply*[ST:LazySegTree](self:var ST, f:ST.F) =\n self.all_apply(1, f)\n proc push*[ST:LazySegTree](self: var ST, k:int) =\n self.all_apply(2 * k, self.lz[k])\n self.all_apply(2 * k + 1, self.lz[k])\n self.lz[k] = ST.calc_id()\n\n proc init[ST:LazySegTree](self:var ST, v:seq[ST.S]) =\n let\n n = v.len\n log = ceil_pow2(n)\n size = 1 shl log\n (self.len, self.size, self.log) = (n, size, log)\n if self.d.len < 2 * size:\n self.d = newSeqWith(2 * size, ST.calc_e())\n else:\n self.d.fill(0, 2 * size - 1, ST.calc_e())\n for i in 0..<n:\n self.d[size + i] = v[i]\n if self.lz.len < size:\n self.lz = newSeqWith(size, ST.calc_id())\n else:\n self.lz.fill(0, size - 1, ST.calc_id())\n for i in countdown(size - 1, 1): self.update(i)\n proc init*[ST:LazySegTree](self: var ST, n:int) = self.init(newSeqWith(n, ST.calc_e()))\n proc init*[ST:LazySegTree](self: typedesc[ST], v:seq[ST.S] or int):ST = result.init(v)\n\n template LazySegTreeType[S, F](op0, e0, mapping0, composition0, id0:untyped):typedesc[LazySegTree] =\n proc op1(a, b:S):S {.gensym inline.} = op0(a, b)\n proc e1():S {.gensym inline.} = e0()\n proc mapping1(f:F, s:S):S {.gensym inline.} = mapping0(f, s)\n proc composition1(f1, f2:F):F {.gensym inline.} = composition0(f1, f2)\n proc id1():F {.gensym inline.} = id0()\n LazySegTree[S, F, (op:op1, e:e1, mapping:mapping1, composition:composition1, id:id1)]\n\n template getType*(ST:typedesc[LazySegTree], S, F:typedesc, op, e, mapping, composition, id:untyped):typedesc[LazySegTree] =\n LazySegTreeType[S, F](op, e, mapping, composition, id)\n\n template initLazySegTree*[S, F](v:seq[S] or int, op, e, mapping, composition, id:untyped):auto =\n LazySegTreeType[S, F](op, e, mapping, composition, id).init(v)\n\n proc set*[ST:LazySegTree](self: var ST, p:IndexType, x:ST.S) =\n var p = self^^p\n assert p in 0..<self.len\n p += self.size\n for i in countdown(self.log, 1): self.push(p shr i)\n self.d[p] = x\n for i in 1..self.log: self.update(p shr i)\n\n proc get*[ST:LazySegTree](self: var ST, p:IndexType):ST.S =\n var p = self^^p\n assert p in 0..<self.len\n p += self.size\n for i in countdown(self.log, 1): self.push(p shr i)\n return self.d[p]\n\n proc `[]=`*[ST:LazySegTree](self: var ST, p:IndexType, x:ST.S) = self.set(p, x)\n proc `[]`*[ST:LazySegTree](self: var ST, p:IndexType):ST.S = self.get(p)\n\n proc prod*[ST:LazySegTree](self:var ST, p:RangeType):ST.S =\n var (l, r) = self.halfOpenEndpoints(p)\n assert 0 <= l and l <= r and r <= self.len\n if l == r: return ST.calc_e()\n\n l += self.size\n r += self.size\n\n for i in countdown(self.log, 1):\n if ((l shr i) shl i) != l: self.push(l shr i)\n if ((r shr i) shl i) != r: self.push(r shr i)\n\n var sml, smr = ST.calc_e()\n while l < r:\n if (l and 1) != 0: sml = ST.calc_op(sml, self.d[l]);l.inc\n if (r and 1) != 0: r.dec;smr = ST.calc_op(self.d[r], smr)\n l = l shr 1\n r = r shr 1\n return ST.calc_op(sml, smr)\n\n proc `[]`*[ST:LazySegTree](self: var ST, p:RangeType):ST.S = self.prod(p)\n\n proc all_prod*[ST:LazySegTree](self:ST):auto = self.d[1]\n\n proc apply*[ST:LazySegTree](self: var ST, p:IndexType, f:ST.F) =\n var p = self^^p\n assert p in 0..<self.len\n p += self.size\n for i in countdown(self.log, 1): self.push(p shr i)\n self.d[p] = ST.calc_mapping(f, self.d[p])\n for i in 1..self.log: self.update(p shr i)\n proc apply*[ST:LazySegTree](self: var ST, p:RangeType, f:ST.F) =\n var (l, r) = self.halfOpenEndpoints(p)\n assert 0 <= l and l <= r and r <= self.len\n if l == r: return\n\n l += self.size\n r += self.size\n\n for i in countdown(self.log, 1):\n if ((l shr i) shl i) != l: self.push(l shr i)\n if ((r shr i) shl i) != r: self.push((r - 1) shr i)\n\n block:\n var (l, r) = (l, r)\n while l < r:\n if (l and 1) != 0: self.all_apply(l, f);l.inc\n if (r and 1) != 0: r.dec;self.all_apply(r, f)\n l = l shr 1\n r = r shr 1\n\n for i in 1..self.log:\n if ((l shr i) shl i) != l: self.update(l shr i)\n if ((r shr i) shl i) != r: self.update((r - 1) shr i)\n\n# template <bool (*g)(S)> int max_right(int l) {\n# return max_right(l, [](S x) { return g(x); });\n# }\n proc max_right*[ST:LazySegTree](self:var ST, l:IndexType, g:proc(s:ST.S):bool):int =\n var l = self^^l\n assert l in 0..self.len\n assert g(ST.calc_e())\n if l == self.len: return self.len\n l += self.size\n for i in countdown(self.log, 1): self.push(l shr i)\n var sm = ST.calc_e()\n while true:\n while l mod 2 == 0: l = l shr 1\n if not g(ST.calc_op(sm, self.d[l])):\n while l < self.size:\n self.push(l)\n l = (2 * l)\n if g(ST.calc_op(sm, self.d[l])):\n sm = ST.calc_op(sm, self.d[l])\n l.inc\n return l - self.size\n sm = ST.calc_op(sm, self.d[l])\n l.inc\n if not((l and -l) != l): break\n return self.len\n\n# template <bool (*g)(S)> int min_left(int r) {\n# return min_left(r, [](S x) { return g(x); });\n# }\n proc min_left*[ST:LazySegTree](self: var ST, r:IndexType, g:proc(s:ST.S):bool):int =\n var r = self^^r\n assert r in 0..self.len\n assert(g(ST.calc_e()))\n if r == 0: return 0\n r += self.size\n for i in countdown(self.log, 1): self.push((r - 1) shr i)\n var sm = ST.calc_e()\n while true:\n r.dec\n while r > 1 and r mod 2 == 1: r = r shr 1\n if not g(ST.calc_op(self.d[r], sm)):\n while r < self.size:\n self.push(r)\n r = (2 * r + 1)\n if g(ST.calc_op(self.d[r], sm)):\n sm = ST.calc_op(self.d[r], sm)\n r.dec\n return r + 1 - self.size\n sm = ST.calc_op(self.d[r], sm)\n if not ((r and -r) != r): break\n return 0\n {.pop.}\n" import sequtils type MergeTree = object ein : seq[int] eout : seq[int] et : seq[int] ret : seq[int] tree : UnWeightedUnDirectedGraph uf : UnionFind now : seq[int] N : int alr_query : int v:seq[(int,int)] proc initMergeTree(N:int,v:seq[(int,int)]):MergeTree= var tree = initUnWeightedUnDirectedGraph(N+len(v)+1) var uf = initUnionFind(N) var now = newseqwith(N,0) for i in 0..<(N): now[i] = i for i in 0..<len(v): var (u,v) = v[i] var x = now[uf.root(u)] var y = now[uf.root(v)] tree.add_edge(x,N+i) if x != y: tree.add_edge(y,N+i) uf.unite(u,v) now[uf.root(u)] = N+i var alr = newSeqWith(N,false) for i in 0..<N: if not alr[uf.root(i)]: alr[uf.root(i)] = true tree.add_edge(N+len(v),now[uf.root(i)]) var ein = newseqwith(N+len(v)+1,-1) var eout = newseqwith(N+len(v)+1,-1) var et : seq[int] proc dfs(x,p:int)= ein[x] = len(et) if x < N: et.add(x) for y in tree[x]: if y != p: dfs(y,x) eout[x] = len(et) dfs(N+len(v),-1) result.tree = move(tree) result.ein = move(ein) result.eout = move(eout) result.et = move(et) result.ret = newseqwith(N,-1) result.uf = initUnionFind(N) result.now = newseqwith(N,0) result.v = v result.N = N for i in 0..<(N): result.now[i] = i for i in 0..<N: result.ret[result.et[i]] = i proc unite(self:var MergeTree,u,v:int)= assert (self.v[self.alr_query][0] == u or self.v[self.alr_query][0] == v) and (self.v[self.alr_query][1] == v or self.v[self.alr_query][1] == u) self.uf.unite(u,v) self.now[self.uf.root(u)] = self.N+self.alr_query self.alr_query += 1 proc get_id(self:MergeTree,x:int):int= return self.now[self.uf.root(x)] proc get_range(self:MergeTree,x:int):HSlice[int,int]= return (self.ein[self.now[self.uf.root(x)]]..<self.eout[self.now[self.uf.root(x)]]) proc make_seq[T](self:MergeTree,v:seq[T]):seq[T]= assert len(v) == self.N result = newseq[T](len(v)) for i in 0..<self.N: result[self.ret[i]] = v[i] proc restore_seq[T](self:MergeTree,v:seq[T]):seq[T]= assert len(v) == self.N result = newseq[T](len(v)) for i in 0..<self.N: result[self.et[i]] = v[i] proc index(self:MergeTree,x:int):int= self.ret[x] proc initRangeAddRangeMaxSegtree[T](v:seq[T]):auto= type S = T type F = T proc op(a,b:S):S=max(a,b) proc e():S=S(-8e18) proc mapping(f:F,x:S):S=f+x proc composition(f,g:F):F=f+g proc id():F=0 return LazySegTree.getType(S, F, op, e, mapping, composition, id).init(v) {.checks:off.} var N,Q = ii() var querys : seq[(int,int,int)] var v : seq[(int,int)] for i in range(Q): var T,A,B = ii() querys.add((T,A,B)) if T == 1: v.add((A-1,B-1)) var MT = initMergeTree(N,v) var st = initRangeAddRangeMaxSegtree(newseqwith(N,0)) for i in range(Q): var (T,A,B) = querys[i] if T == 1: MT.unite(A-1,B-1) elif T == 2: st.apply(MT.get_range(A-1),B) else: stdout.writeLine st[MT.index(A-1)]