結果

問題 No.833 かっこいい電車
ユーザー titiatitia
提出日時 2019-05-29 00:07:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 562 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 94,932 KB
最終ジャッジ日時 2023-09-14 21:04:37
合計ジャッジ時間 8,278 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 239 ms
91,704 KB
testcase_01 AC 72 ms
71,136 KB
testcase_02 AC 72 ms
71,392 KB
testcase_03 AC 73 ms
71,368 KB
testcase_04 AC 73 ms
71,332 KB
testcase_05 AC 72 ms
71,380 KB
testcase_06 AC 71 ms
71,340 KB
testcase_07 AC 73 ms
71,168 KB
testcase_08 AC 75 ms
71,560 KB
testcase_09 AC 73 ms
71,388 KB
testcase_10 AC 277 ms
92,092 KB
testcase_11 AC 305 ms
94,932 KB
testcase_12 AC 190 ms
84,324 KB
testcase_13 AC 204 ms
82,344 KB
testcase_14 AC 270 ms
91,540 KB
testcase_15 AC 214 ms
85,992 KB
testcase_16 AC 175 ms
86,092 KB
testcase_17 AC 223 ms
84,088 KB
testcase_18 AC 291 ms
93,776 KB
testcase_19 AC 178 ms
84,004 KB
testcase_20 AC 104 ms
78,972 KB
testcase_21 AC 282 ms
91,504 KB
testcase_22 AC 206 ms
89,188 KB
testcase_23 AC 191 ms
85,028 KB
testcase_24 AC 221 ms
89,060 KB
testcase_25 AC 291 ms
91,984 KB
testcase_26 AC 174 ms
86,488 KB
testcase_27 AC 257 ms
88,640 KB
testcase_28 AC 245 ms
86,200 KB
testcase_29 AC 238 ms
87,492 KB
testcase_30 AC 190 ms
92,944 KB
testcase_31 AC 235 ms
91,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import bisect
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)))
        #print()
        #print(BIT,SEG)
        #print(s)
        #print((lower_bound(s),lower_bound(s+1)))
        #print()
        
        
0