結果
問題 | No.789 範囲の合計 |
ユーザー | むらため |
提出日時 | 2019-02-11 06:11:53 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 89 ms / 1,000 ms |
コード長 | 2,386 bytes |
コンパイル時間 | 3,335 ms |
コンパイル使用メモリ | 75,904 KB |
実行使用メモリ | 12,416 KB |
最終ジャッジ日時 | 2024-07-01 19:21:27 |
合計ジャッジ時間 | 4,833 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 16 ms
6,144 KB |
testcase_01 | AC | 15 ms
6,272 KB |
testcase_02 | AC | 82 ms
12,032 KB |
testcase_03 | AC | 43 ms
8,576 KB |
testcase_04 | AC | 77 ms
12,032 KB |
testcase_05 | AC | 66 ms
12,160 KB |
testcase_06 | AC | 70 ms
12,032 KB |
testcase_07 | AC | 36 ms
8,576 KB |
testcase_08 | AC | 53 ms
9,984 KB |
testcase_09 | AC | 50 ms
9,984 KB |
testcase_10 | AC | 89 ms
12,416 KB |
testcase_11 | AC | 71 ms
12,160 KB |
testcase_12 | AC | 71 ms
12,032 KB |
testcase_13 | AC | 16 ms
6,272 KB |
testcase_14 | AC | 15 ms
6,272 KB |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport] /home/judge/data/code/Main.nim(1, 33) Warning: imported and not used: 'bitops' [UnusedImport]
ソースコード
import sequtils,algorithm,times,bitops template stopwatch(body) = (let t1 = cpuTime();body;stderr.writeLine "TIME:",(cpuTime() - t1) * 1000,"ms") 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 template useBinaryIndexedTree() = # 部分和検索 / 更新 O(log(N)) type BinaryIndexedTree[T] = ref object data: seq[T] # 引数以下の部分和(Fenwick Tree) proc initBinaryIndexedTree[T](n:int):BinaryIndexedTree[T] = new(result) result.data = newSeq[T](n) proc len[T](self:BinaryIndexedTree[T]): int = self.data.len() proc update[T](self:var BinaryIndexedTree[T],i:int,val:T) = var i = i while i < self.len(): self.data[i] += val i = i or (i + 1) proc `[]`[T](self:BinaryIndexedTree[T],i:int): T = var i = i while i >= 0: result += self.data[i] i = (i and (i + 1)) - 1 proc `$`[T](self:BinaryIndexedTree[T]): string = result = "[" for i in 0..<self.len.min(100): result &= $(self[i]) & ", " return result[0..result.len()-2] & (if self.len > 100 : " ...]" else: "]") proc quickSort[T](a: var openarray[T], inl = 0, inr = -1) = var r = if inr >= 0: inr else: a.high var l = inl let n = r - l + 1 if n < 2: return let p = a[l + 3 * n div 4] while l <= r: if a[l] < p: inc l continue if a[r] > p: dec r continue if l <= r: swap a[l], a[r] inc l dec r quickSort(a, inl, r) quickSort(a, l, inr) proc deduplicated[T](arr: openarray[T]): seq[T] = # require sorted result = @[] for a in arr: if result.len > 0 and result[^1] == a : continue result &= a useBinaryIndexedTree() stopwatch: let n = scan() var X,Y,Q : array[100010,int] var V : array[200010,int] for i in 0..<n: Q[i] = scan() X[i] = scan() Y[i] = scan() V[i*2] = X[i] # V &= X[i] if Q[i] == 1 : V[i*2+1] = Y[i] stopwatch: V.quicksort() stopwatch: var V2 = V.deduplicated() stopwatch: var bit = initBinaryIndexedTree[int](V.len + 1) var ans = 0 for i in 0..<n: let x = V2.lowerBound(X[i]) if Q[i] == 0: bit.update(x,Y[i]) else: let y = V2.lowerBound(Y[i]) if x == 0: ans += bit[y] else: ans += bit[y] - bit[x-1] echo ans