結果

問題 No.876 Range Compress Query
ユーザー むらためむらため
提出日時 2019-09-07 08:17:10
言語 Nim
(2.0.2)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 1,644 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 66,344 KB
実行使用メモリ 5,400 KB
最終ジャッジ日時 2023-09-15 12:53:54
合計ジャッジ時間 4,533 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 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,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 107 ms
4,892 KB
testcase_12 AC 87 ms
4,584 KB
testcase_13 AC 88 ms
4,796 KB
testcase_14 AC 107 ms
4,900 KB
testcase_15 AC 74 ms
5,028 KB
testcase_16 AC 102 ms
5,268 KB
testcase_17 AC 103 ms
5,400 KB
testcase_18 AC 108 ms
5,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils
template times*(n:int,body) = (for _ in 0..<n: body)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>",discardable .}
proc scan(): int =
  while true:
    var k = getchar_unlocked()
    if k < '0' or k > '9': break
    result = 10 * result + k.ord - '0'.ord

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
    if dal xor (A[l-1] == A[l]):
      if dal : D.plus l,1
      else: D.plus l,-1
    if dar xor (A[r-1] == A[r]):
      if dar : D.plus r,1
      else: D.plus r,-1
  else:
    let (l,r) = (scan(),scan())
    echo D[r]-D[l]+1
0