結果
問題 | No.875 Range Mindex Query |
ユーザー |
|
提出日時 | 2019-09-06 21:54:19 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 241 ms / 2,000 ms |
コード長 | 2,464 bytes |
コンパイル時間 | 2,473 ms |
コンパイル使用メモリ | 61,272 KB |
実行使用メモリ | 6,912 KB |
最終ジャッジ日時 | 2024-07-02 16:06:24 |
合計ジャッジ時間 | 5,214 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 18 |
ソースコード
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