結果

問題 No.833 かっこいい電車
ユーザー titiatitia
提出日時 2019-05-29 00:09:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 893 ms / 2,000 ms
コード長 2,075 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 33,408 KB
最終ジャッジ日時 2023-09-14 21:05:40
合計ジャッジ時間 14,341 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 893 ms
30,196 KB
testcase_01 AC 16 ms
8,076 KB
testcase_02 AC 16 ms
8,100 KB
testcase_03 AC 17 ms
8,232 KB
testcase_04 AC 17 ms
8,048 KB
testcase_05 AC 17 ms
8,056 KB
testcase_06 AC 17 ms
8,084 KB
testcase_07 AC 17 ms
8,112 KB
testcase_08 AC 17 ms
8,172 KB
testcase_09 AC 16 ms
8,000 KB
testcase_10 AC 673 ms
27,748 KB
testcase_11 AC 891 ms
33,408 KB
testcase_12 AC 279 ms
17,064 KB
testcase_13 AC 197 ms
14,268 KB
testcase_14 AC 681 ms
29,324 KB
testcase_15 AC 335 ms
19,136 KB
testcase_16 AC 221 ms
17,000 KB
testcase_17 AC 258 ms
16,212 KB
testcase_18 AC 805 ms
31,104 KB
testcase_19 AC 232 ms
16,548 KB
testcase_20 AC 68 ms
11,124 KB
testcase_21 AC 716 ms
27,952 KB
testcase_22 AC 408 ms
23,376 KB
testcase_23 AC 271 ms
18,312 KB
testcase_24 AC 420 ms
23,336 KB
testcase_25 AC 809 ms
30,328 KB
testcase_26 AC 314 ms
19,676 KB
testcase_27 AC 526 ms
24,120 KB
testcase_28 AC 414 ms
20,268 KB
testcase_29 AC 465 ms
22,344 KB
testcase_30 AC 620 ms
32,276 KB
testcase_31 AC 868 ms
28,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
A=list(map(int,input().split()))
Query=[list(map(int,input().split())) for i in range(Q)]

#Segment tree(1-indexed,再帰を使わないもの)

def seg_function(x,y):#Segment treeで扱うfunction
    return x+y

seg_el=1<<(N.bit_length())#Segment treeの台の要素数
SEG=[0]*(2*seg_el)#1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

for i in range(N):#Aを対応する箇所へupdate
    SEG[i+seg_el]=A[i]

for i in range(seg_el-1,0,-1):#親の部分もupdate
    SEG[i]=seg_function(SEG[i*2],SEG[i*2+1])

def update(n,x,seg_el):#A[n]を+x更新(反映)
    i=n+seg_el
    SEG[i]+=x
    i>>=1#子ノードへ
    
    while i!=0:
        SEG[i]=seg_function(SEG[i*2],SEG[i*2+1])
        i>>=1
        
def getvalues(l,r):#区間[l,r)に関するseg_functionを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=0

    while L<R:
        if L & 1:
            ANS=seg_function(ANS , SEG[L])
            L+=1

        if R & 1:
            R-=1
            ANS=seg_function(ANS , SEG[R])
        L>>=1
        R>>=1

    return ANS

LIST=[1]*N
BIT=[0]*(N+2)
BL=(N+2).bit_length()

def update_b(v,w):#vにwを加える
    while v<=N+1:
        BIT[v]+=w
        v+=(v&(-v))#たとえばv=3→v=4へ

def getvalue_b(v):#MIN~vの区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v))#たとえばv=3→v=2へ
    return ANS

def lower_bound(x):
    w=0
    for i in range(BL-1,-1,-1):
        if w+(1<<i)<N+1 and BIT[w+(1<<i)]<x:
            x-=BIT[w+(1<<i)]
            w+=(1<<i)

    return w


for i in range(1,N+2):
    update_b(i,1)

for q,x in Query:
    if q==1:
        if LIST[x-1]==1:
            LIST[x-1]=0
            update_b(x+1,-1)

    elif q==2:
        if LIST[x-1]==0:
            LIST[x-1]=1
            update_b(x+1,1)

    elif q==3:
        update(x-1,1,seg_el)

    else:
        
        s=getvalue_b(x)
        #値がsの区間を調べる
        print(getvalues(lower_bound(s),lower_bound(s+1)))

        
0