結果

問題 No.875 Range Mindex Query
ユーザー むらためむらため
提出日時 2019-09-06 21:54:19
言語 Nim
(2.0.2)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 2,464 bytes
コンパイル時間 2,622 ms
コンパイル使用メモリ 66,184 KB
実行使用メモリ 7,072 KB
最終ジャッジ日時 2023-09-15 12:51:47
合計ジャッジ時間 6,636 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 187 ms
7,060 KB
testcase_12 AC 147 ms
4,960 KB
testcase_13 AC 128 ms
7,008 KB
testcase_14 AC 124 ms
7,032 KB
testcase_15 AC 178 ms
7,028 KB
testcase_16 AC 221 ms
7,072 KB
testcase_17 AC 232 ms
6,904 KB
testcase_18 AC 225 ms
7,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils
# import algorithm,math,tables
# import times,macros,queues,bitops,strutils,intsets,sets
# import rationals,critbits,ropes,nre,pegs,complex,stats,heapqueue,sugar
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
# template stopwatch(body) = (let t1 = cpuTime();body;stderr.writeLine "TIME:",(cpuTime() - t1) * 1000,"ms")
# proc `^`(n:int) : int{.inline.} = (1 shl n)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" ,discardable.}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

import sequtils,math
type
  ValWithIndex[T] = tuple[val:T,index:int]
  SegmentTreeWithIndex[T] = ref object
    data:seq[ValWithIndex[T]]
    n : int
    rawSize:int
    infinity: T
    cmp:proc(x,y:ValWithIndex[T]):ValWithIndex[T]
proc newSegmentTree[T](size:int) : SegmentTreeWithIndex[T] =
  new(result)
  result.infinity = 1e12.T
  proc minimpl[T](x,y:ValWithIndex[T]): ValWithIndex[T] =
    if x.val <= y.val: x
    else: y
  result.cmp = minimpl
  result.n = size.nextPowerOfTwo()
  result.rawSize = size
  result.data = newSeqWith(result.n * 2,(result.infinity,-1))
proc `[]=`[T](self:var SegmentTreeWithIndex[T],i:int,val:T) =
  let baseI = i
  var i = i + self.n - 1
  self.data[i] = (val,baseI)
  while i > 0:
    i = (i - 1) shr 1
    self.data[i] = self.cmp(self.data[i * 2 + 1],self.data[i * 2 + 2])
proc queryImpl[T](self:SegmentTreeWithIndex[T],a,b,k,l,r:int) : ValWithIndex[T] =
  if r <= a or b <= l : return (self.infinity,-1)
  if a <= l and r <= b : return self.data[k]
  let vl = self.queryImpl(a,b,k*2+1,l,(l+r) shr 1)
  let vr = self.queryImpl(a,b,k*2+2,(l+r) shr 1,r)
  return self.cmp(vl,vr)
proc `[]`[T](self:SegmentTreeWithIndex[T],slice:Slice[int]): ValWithIndex[T] =
  return self.queryImpl(slice.a,slice.b+1,0,0,self.n)

proc `$`[T](self:SegmentTreeWithIndex[T]): string =
  var arrs : seq[seq[ValWithIndex[T]]] = @[]
  var l = 0
  var r = 1
  while r <= self.data.len:
    arrs.add self.data[l..<r]
    l = l * 2 + 1
    r = r * 2 + 1
  return $arrs


let n = scan()
let q = scan()
var S = newSegmentTree[int](n)
for i in 0..<n: S[i] = scan()
q.times:
  let (x,l,r) = (scan(),scan()-1,scan()-1)
  if x == 1:
    var lx = S[l..l].val
    var rx = S[r..r].val
    S[l] = rx
    S[r] = lx
  else:
    # echo S
    let x = S[l..r]
    echo x.index + 1
0