結果

問題 No.833 かっこいい電車
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-06-21 23:46:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,617 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 97,024 KB
最終ジャッジ日時 2024-04-23 02:12:54
合計ジャッジ時間 7,252 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
85,944 KB
testcase_01 AC 34 ms
52,480 KB
testcase_02 AC 34 ms
51,968 KB
testcase_03 AC 33 ms
52,992 KB
testcase_04 AC 34 ms
52,224 KB
testcase_05 AC 35 ms
52,096 KB
testcase_06 AC 34 ms
52,608 KB
testcase_07 AC 35 ms
52,736 KB
testcase_08 AC 34 ms
52,224 KB
testcase_09 AC 36 ms
52,224 KB
testcase_10 AC 246 ms
86,528 KB
testcase_11 AC 264 ms
92,672 KB
testcase_12 AC 155 ms
82,688 KB
testcase_13 AC 168 ms
78,080 KB
testcase_14 AC 244 ms
94,080 KB
testcase_15 AC 185 ms
85,376 KB
testcase_16 AC 150 ms
89,856 KB
testcase_17 AC 201 ms
78,276 KB
testcase_18 AC 274 ms
86,912 KB
testcase_19 AC 157 ms
87,040 KB
testcase_20 AC 64 ms
74,624 KB
testcase_21 AC 258 ms
81,248 KB
testcase_22 AC 186 ms
95,260 KB
testcase_23 AC 174 ms
88,704 KB
testcase_24 AC 191 ms
94,464 KB
testcase_25 AC 257 ms
85,376 KB
testcase_26 AC 154 ms
89,728 KB
testcase_27 AC 216 ms
86,400 KB
testcase_28 AC 209 ms
81,024 KB
testcase_29 AC 211 ms
84,448 KB
testcase_30 AC 161 ms
97,024 KB
testcase_31 AC 268 ms
85,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def build(self, list):
        self.tree[1:] = list.copy()
        for i in range(self.size+1):
            j = i + (i & (-i))
            if j < self.size+1:
                self.tree[j] += self.tree[i]

    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    # 総和がx以上になる位置のindex をbinary search
    def bsearch(self,x):
        le = 0
        ri = 1<<(self.size.bit_length()-1)
        while ri > 0:
            if le+ri <= self.size and self.tree[le+ri]<x:
                x -= self.tree[le+ri]
                le += ri
            ri >>= 1
        return le+1


n,q = map(int,input().split())
A = list(map(int,input().split()))
bit = BIT(n+5)
bit2 = BIT(n+5)
connect = [0]*n
bit.build([0]+A+[0]*4)
bit2.build([1]*(n+5))
for _ in range(q):
    t,x = map(int,input().split())

    if t == 1:
        if connect[x] == 0:
            connect[x] = 1
            bit2.add(x,-1)
    elif t == 2:
        if connect[x] == 1:
            connect[x] = 0
            bit2.add(x,1)
    
    elif t == 3:
        bit.add(x,1)
    
    else:
        num = bit2.sum(x)

        lind = bit2.bsearch(num)
        rind = bit2.bsearch(num+1)
        print(bit.sum(rind)-bit.sum(lind))
0