結果

問題 No.876 Range Compress Query
ユーザー titiatitia
提出日時 2019-09-07 20:43:28
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
RE  
実行時間 -
コード長 1,285 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 11,136 KB
実行使用メモリ 37,248 KB
最終ジャッジ日時 2023-09-09 11:01:54
合計ジャッジ時間 5,219 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,056 KB
testcase_01 RE -
testcase_02 AC 15 ms
8,212 KB
testcase_03 AC 18 ms
8,024 KB
testcase_04 AC 17 ms
8,144 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 18 ms
8,024 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 570 ms
35,980 KB
testcase_12 AC 473 ms
31,560 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます

ソースコード

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)]

B=[A[i]-A[i-1] for i in range(1,N)]

A=[0 if B[i]==0 else 1 for i in range(N-1)]

# BIT(BIT-indexed tree)

LEN=len(A)+1# 必要なら座標圧縮する

BIT=[0]*(LEN+1)# 1-indexedなtree

def update(v,w):# index vにwを加える
    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v))# 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue(v):# [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v))# 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS

for i in range(N-1):
    if B[i]!=0:
        update(i+1,1)
        

for q in Query:
    if q[0]==1:
        i,l,r,x=q
        B[l-2]+=x
        B[r-1]-=x

        if A[l-2]==0 and B[l-2]!=0:
            A[l-2]=1
            update(l-1,1)

        elif A[l-2]==1 and B[l-2]==0:
            A[l-2]=0
            update(l-1,-1)

        if A[r-1]==0 and B[r-1]!=0:
            A[r-1]=1
            update(r,1)

        elif A[r-1]==1 and B[r-1]==0:
            A[r-1]=0
            update(r,-1)

    else:
        i,l,r=q
        print(getvalue(r-1)-getvalue(l-1)+1)
        
0