結果
| 問題 |
No.876 Range Compress Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-06 23:12:44 |
| 言語 | Nim (2.2.0) |
| 結果 |
AC
|
| 実行時間 | 142 ms / 2,000 ms |
| コード長 | 1,733 bytes |
| コンパイル時間 | 2,559 ms |
| コンパイル使用メモリ | 61,584 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-02 16:06:54 |
| 合計ジャッジ時間 | 4,117 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
import sequtils
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)
proc scanf(formatstr: cstring){.header: "<stdio.h>", varargs.}
proc scan(): int = scanf("%lld\n",addr result)
type BinaryIndexedTree[T] = ref object
data: seq[T]
proc newBinaryIndexedTree[T](n:int):BinaryIndexedTree[T] =
new(result)
result.data = newSeq[T](n)
proc len[T](self:BinaryIndexedTree[T]): int = self.data.len()
proc plus[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: "]")
let n = scan()
let q = scan()
let B = newSeqWith(n,scan())
var A = newBinaryIndexedTree[int](n+2)
var D = newBinaryIndexedTree[int](n+2)
# echo B
for i in 1..<n:
A.plus i+1, B[i] - B[i-1]
for i in 2..n:
if A[i] != A[i-1]: D.plus i, 1
q.times:
let qtype = scan()
if qtype == 1:
let (l,r,x) = (scan(),scan()+1,scan())
let dal = A[l-1] == A[l]
let dar = A[r-1] == A[r]
A.plus l,x
A.plus r,-x
let dal2 = A[l-1] == A[l]
let dar2 = A[r-1] == A[r]
if dal xor dal2:
if dal : D.plus l,1
else: D.plus l,-1
if dar xor dar2:
if dar : D.plus r,1
else: D.plus r,-1
# echo A
# echo D
# echo l," ",r," ",x
else:
let (l,r) = (scan(),scan())
# echo A
# echo D
# echo l," ",r#," ",x
echo D[r]-D[l]+1