結果

問題 No.876 Range Compress Query
ユーザー pynomipynomi
提出日時 2019-09-07 17:33:45
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,488 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 109,148 KB
最終ジャッジ日時 2023-09-09 05:49:23
合計ジャッジ時間 10,430 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,136 KB
testcase_01 RE -
testcase_02 AC 84 ms
76,208 KB
testcase_03 AC 123 ms
77,720 KB
testcase_04 AC 94 ms
76,660 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 117 ms
77,652 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 1,207 ms
109,032 KB
testcase_12 AC 921 ms
105,164 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, N, init, operation):
        self.N = 2**(N-1).bit_length()
        self.data = [init] * (2*self.N)
        self.init = init
        self.operation = operation

    def update(self, k, x):
        k += self.N-1
        self.data[k] = x
        while k >= 0:
            k = (k - 1) // 2
            self.data[k] = self.operation((self.data[2*k+1], self.data[2*k+2]))
    
    def query(self, l, r):
        L = l + self.N
        R = r + self.N
        s = self.init
        while L < R:
            if R & 1:
                R -= 1
                s = self.operation((s, self.data[R-1]))
            if L & 1:
                s = self.operation((s, self.data[L-1]))
                L += 1
            L >>= 1; R >>= 1
        return s

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


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

st_sum = SegmentTree(N, 0, sum)

for k, b in enumerate(B):
    if b != 0:
        st_sum.update(k, 1)
    else:
        st_sum.update(k, 0)

for line in query:
    if len(line) == 4:
        q, l, r, x = line
        l -= 1
        r -= 1
        if l != 0:
            B[l-1] += x
        st_sum.update(l-1, B[l-1] != 0)
        if l != N:
            B[r] -= x
        st_sum.update(r, B[r] != 0)
    elif len(line) == 3:
        q, l, r = line
        l -= 1
        r -= 1
        ans = st_sum.query(l, r) + 1
        print(ans)
0