結果

問題 No.876 Range Compress Query
ユーザー pynomipynomi
提出日時 2019-09-07 18:23:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 1,270 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 98,528 KB
最終ジャッジ日時 2023-09-09 07:17:09
合計ジャッジ時間 5,864 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,484 KB
testcase_01 AC 92 ms
76,516 KB
testcase_02 AC 76 ms
75,584 KB
testcase_03 AC 98 ms
76,604 KB
testcase_04 AC 80 ms
75,676 KB
testcase_05 AC 78 ms
75,652 KB
testcase_06 AC 98 ms
76,564 KB
testcase_07 AC 93 ms
76,460 KB
testcase_08 AC 93 ms
76,508 KB
testcase_09 AC 90 ms
76,372 KB
testcase_10 AC 90 ms
76,572 KB
testcase_11 AC 393 ms
98,528 KB
testcase_12 AC 359 ms
96,032 KB
testcase_13 AC 354 ms
95,424 KB
testcase_14 AC 395 ms
98,128 KB
testcase_15 AC 332 ms
93,048 KB
testcase_16 AC 399 ms
97,144 KB
testcase_17 AC 402 ms
97,124 KB
testcase_18 AC 418 ms
98,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def update(self, k, x):
        k += self.N-1
        self.data[k] = x
        while k >= 0:
            k = (k - 1) // 2
            self.data[k] = self.data[2*k+1] + self.data[2*k+2]
    
    def query(self, l, r):
        L = l + self.N
        R = r + self.N
        s = 0
        while L < R:
            if R & 1:
                R -= 1
                s += self.data[R-1]
            if L & 1:
                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 = SegmentTree(N)

for i, b in enumerate(B):
    st.update(i, b != 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.update(l-1, B[l-1] != 0)
        if r != N:
            B[r] -= x
            st.update(r, B[r] != 0)
    elif len(line) == 3:
        q, l, r = line
        l -= 1
        r -= 1
        ans = st.query(l, r) + 1
        print(ans)
0