結果

問題 No.876 Range Compress Query
ユーザー 6soukiti296soukiti29
提出日時 2019-09-06 22:32:05
言語 Nim
(2.0.2)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 1,322 bytes
コンパイル時間 3,045 ms
コンパイル使用メモリ 68,688 KB
実行使用メモリ 15,048 KB
最終ジャッジ日時 2023-09-15 12:52:13
合計ジャッジ時間 6,084 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,588 KB
testcase_01 AC 4 ms
4,632 KB
testcase_02 AC 3 ms
4,544 KB
testcase_03 AC 4 ms
4,544 KB
testcase_04 AC 4 ms
4,592 KB
testcase_05 AC 3 ms
4,740 KB
testcase_06 AC 4 ms
4,584 KB
testcase_07 AC 4 ms
4,660 KB
testcase_08 AC 4 ms
4,648 KB
testcase_09 AC 4 ms
4,692 KB
testcase_10 AC 4 ms
4,772 KB
testcase_11 AC 188 ms
13,028 KB
testcase_12 AC 155 ms
12,744 KB
testcase_13 AC 153 ms
13,168 KB
testcase_14 AC 185 ms
12,996 KB
testcase_15 AC 130 ms
13,796 KB
testcase_16 AC 176 ms
15,048 KB
testcase_17 AC 176 ms
14,984 KB
testcase_18 AC 188 ms
14,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,strutils
type
    binaryIndexTree[I:static[int]] = array[I + 1,int64] 

proc add(BIT :var binaryIndexTree,i : int64, a : int64)=
    var index = i
    while BIT[0] >= index:
        BIT[index] += a
        index += ((index xor (index - 1)) and index)

proc sum(BIT : binaryIndexTree, i : int64):int64 =
    var index = i
    while index > 0:
        result += BIT[index]
        index = (index and (index - 1))

proc initBit(i : static[int]) : binaryIndexTree[i] =
    var res : binaryIndexTree[i]
    res[0] = i
    return res

var
    N, Q : int64
(N, Q) = stdin.readline.split.map(parseBiggestInt)
var
    BIT = initBit(100010)
    A = stdin.readline.split.map(parseBiggestInt)
    S : array[100010, int64]

for i, a in A:
    if i == 0:
        continue
    S[i] = a - A[i - 1]
    if S[i] != 0:
        BIT.add(i, 1)

for q in 1..Q:
    var L = stdin.readline.split.map(parseBiggestInt)
    var
        l = L[1]
        r = L[2]
    if L[0] == 1:
        var x = L[3]
        S[l - 1] += x
        S[r] -= x
        if l > 2 and S[l - 1] == 0:
            BIT.add(l - 1, -1)
        elif l > 2 and S[l - 1] == x:
            BIT.add(l - 1, 1)
        if S[r] == 0:
            BIT.add(r, -1)
        elif S[r] == -x:
            BIT.add(r, 1)
    else:
        echo BIT.sum(r - 1) - BIT.sum(l - 1) + 1
0