結果
| 問題 | No.789 範囲の合計 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-11 05:54:12 |
| 言語 | Nim (2.2.6) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,394 bytes |
| 記録 | |
| コンパイル時間 | 3,548 ms |
| コンパイル使用メモリ | 76,032 KB |
| 実行使用メモリ | 14,080 KB |
| 最終ジャッジ日時 | 2024-07-01 19:21:17 |
| 合計ジャッジ時間 | 7,207 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 TLE * 1 -- * 6 |
コンパイルメッセージ
/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[A](xs: var seq[A],a,b:int) =
proc pivot[A](xs: var seq[A],a,b:int): int =
if xs.len < 2 : return 0
var pi = a
let p = xs[pi]
for i in a+1..b:
if xs[i] >= p or pi >= i : continue
swap(xs[i], xs[pi+1])
swap(xs[pi], xs[pi+1])
pi = pi + 1
return pi
if b-a <= 1: return
let p = pivot(xs,a,b)
quicksort(xs, a , p)
quicksort(xs, p+1 , b)
proc deduplicated[T](arr: seq[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 = newSeq[int](n)
var Y = newSeq[int](n)
var Q = newSeq[int](n)
var V = newSeq[int]()
for i in 0..<n:
Q[i] = scan()
X[i] = scan()
Y[i] = scan()
V &= X[i]
if Q[i] == 1 : V &= Y[i]
stopwatch:
V.quicksort(V.low,V.high)
V = V.deduplicated()
stopwatch:
var bit = initBinaryIndexedTree[int](V.len + 1)
var ans = 0
for i in 0..<n:
let x = V.lowerBound(X[i])
if Q[i] == 0: bit.update(x,Y[i])
else:
let y = V.lowerBound(Y[i])
if x == 0: ans += bit[y]
else: ans += bit[y] - bit[x-1]
echo ans