結果

問題 No.876 Range Compress Query
ユーザー titia
提出日時 2019-09-07 20:43:28
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 1,285 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 39,908 KB
最終ジャッジ日時 2024-06-27 04:09:50
合計ジャッジ時間 5,903 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 6 RE * 12
権限があれば一括ダウンロードができます

ソースコード

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