結果

問題 No.833 かっこいい電車
ユーザー 6soukiti296soukiti29
提出日時 2019-09-07 13:07:08
言語 Nim
(2.0.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,740 bytes
コンパイル時間 3,276 ms
コンパイル使用メモリ 68,968 KB
実行使用メモリ 16,444 KB
最終ジャッジ日時 2023-09-15 12:54:35
合計ジャッジ時間 7,001 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
10,352 KB
testcase_01 AC 4 ms
5,492 KB
testcase_02 AC 4 ms
5,368 KB
testcase_03 AC 3 ms
5,388 KB
testcase_04 AC 3 ms
5,492 KB
testcase_05 AC 4 ms
5,372 KB
testcase_06 AC 3 ms
5,340 KB
testcase_07 AC 3 ms
5,400 KB
testcase_08 AC 3 ms
5,320 KB
testcase_09 AC 3 ms
5,332 KB
testcase_10 AC 112 ms
10,544 KB
testcase_11 AC 143 ms
14,184 KB
testcase_12 AC 45 ms
9,056 KB
testcase_13 AC 38 ms
6,552 KB
testcase_14 AC 107 ms
14,052 KB
testcase_15 AC 56 ms
10,436 KB
testcase_16 AC 37 ms
12,024 KB
testcase_17 AC 53 ms
6,028 KB
testcase_18 AC 141 ms
11,312 KB
testcase_19 AC 38 ms
11,280 KB
testcase_20 AC 11 ms
7,448 KB
testcase_21 AC 130 ms
8,080 KB
testcase_22 AC 63 ms
15,156 KB
testcase_23 AC 45 ms
11,832 KB
testcase_24 AC 64 ms
14,272 KB
testcase_25 AC 136 ms
10,516 KB
testcase_26 AC 46 ms
13,156 KB
testcase_27 AC 90 ms
10,560 KB
testcase_28 AC 75 ms
7,828 KB
testcase_29 AC 80 ms
9,772 KB
testcase_30 AC 82 ms
16,444 KB
testcase_31 AC 143 ms
10,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

proc sum(BIT : binaryIndexTree, i : int):int =
    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, k, x : int
(N, Q) = stdin.readline.split.map(parseInt)

var
    BITf = initBit(100010)
    BITa = initBit(100010)
    F : array[100010, bool]
    A = @[0] & stdin.readline.split.map(parseInt)

for i in 1..N:
    BITf.add(i, 1)
    BITa.add(i, A[i])

for q in 1..Q:
    (k, x) = stdin.readline.split.map(parseInt)
    if k == 1 and F[x + 1] == false:
        F[x + 1] = true
        BITf.add(x + 1, -1)
    elif k == 2 and F[x + 1] == true:
        F[x + 1] = false
        BITf.add(x + 1, 1)
    elif k == 3:
        BITa.add(x, 1)
    elif k == 4:
        var m = 1
        var M = x
        var l, r : int
        if BITf.sum(1) == BITf.sum(x):
            M = 1
        while M - m > 1:
            var mid = (M + m) div 2
            if BITf.sum(mid) == BITf.sum(x):
                M = mid
            else:
                m = mid
        l = M
        
        m = x
        M = N
        if BITf.sum(x) == BITf.sum(N):
            m = N
            
        while M - m > 1:
            var mid = (M + m) div 2
            if BITf.sum(mid) == BITf.sum(x):
                m = mid
            else:
                M = mid
        r = m
        echo BITa.sum(r) - BITa.sum(l - 1)



0