import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) # verification-helper: PROBLEM https://yukicoder.me/problems/no/878 import cplib/collections/segtree ImportExpand "cplib/collections/segtree.nim" <=== "when not declared CPLIB_COLLECTIONS_SEGTREE:\n const CPLIB_COLLECTIONS_SEGTREE* = 1\n import algorithm\n import strutils\n type SegmentTree*[T] = ref object\n default: T\n merge: proc(x: T, y: T): T\n arr*: seq[T]\n lastnode: int\n length: int\n proc initSegmentTree*[T](v: seq[T], merge: proc(x: T, y: T): T, default: T): SegmentTree[T] =\n ## セグメントツリーを生成します。\n ## vに元となるリスト、mergeに二つの区間をマージする関数、デフォルトに単位元を与えてください。\n var lastnode = 1\n while lastnode < len(v):\n lastnode*=2\n var arr = newSeq[T](2*lastnode)\n arr.fill(default)\n var self = SegmentTree[T](default: default, merge: merge, arr: arr, lastnode: lastnode, length: len(v))\n #1-indexedで作成する\n for i in 0.. 1:\n x = x shr 1\n self.arr[x] = self.merge(self.arr[2*x], self.arr[2*x+1])\n proc get*[T](self: SegmentTree[T], q_left: Natural, q_right: Natural): T =\n ## 半解区間[q_left,q_right)についての演算結果を返します。\n assert q_left <= q_right and 0 <= q_left and q_right <= self.length\n var q_left = q_left\n var q_right = q_right\n q_left += self.lastnode\n q_right += self.lastnode\n var (lres, rres) = (self.default, self.default)\n while q_left < q_right:\n if (q_left and 1) > 0:\n lres = self.merge(lres, self.arr[q_left])\n q_left += 1\n if (q_right and 1) > 0:\n q_right -= 1\n rres = self.merge(self.arr[q_right], rres)\n q_left = q_left shr 1\n q_right = q_right shr 1\n return self.merge(lres, rres)\n proc get*[T](self: SegmentTree[T], segment: HSlice[int, int]): T =\n assert segment.a <= segment.b + 1 and 0 <= segment.a and segment.b+1 <= self.length\n return self.get(segment.a, segment.b+1)\n proc `[]`*[T](self: SegmentTree[T], segment: HSlice[int, int]): T = self.get(segment)\n proc `[]`*[T](self: SegmentTree[T], index: Natural): T =\n assert index < self.length\n return self.arr[index+self.lastnode]\n proc `[]=`*[T](self: SegmentTree[T], index: Natural, val: T) =\n assert index < self.length\n self.update(index, val)\n proc get_all*[T](self: SegmentTree[T]): T =\n ## [0,len(self))区間の演算結果をO(1)で返す\n return self.arr[1]\n proc len*[T](self: SegmentTree[T]): int =\n return self.length\n proc `$`*[T](self: SegmentTree[T]): string =\n var s = self.arr.len div 2\n return self.arr[s.. 1 and r mod 2 != 0): r = (r shr 1)\n if not f(self.merge(self.arr[r], sm)):\n while r < self.lastnode:\n r = 2 * r + 1\n if f(self.merge(self.arr[r], sm)):\n sm = self.merge(self.arr[r], sm)\n r -= 1\n return r + 1 - self.lastnode\n if (r and -r) == r: break\n return 0\n" import sequtils import strutils import algorithm proc scanf(formatstr: cstring){.header: "", varargs.} proc ii(): int {.inline.} = scanf("%lld\n", addr result) var n, q = ii() var a = newSeqWith(n, ii()) var seg = newSegWith(a, max(l, r), 0) var mn = newSeq[int](n) for i in 0..