結果

問題 No.876 Range Compress Query
ユーザー pynomipynomi
提出日時 2019-09-07 02:06:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 419 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 98,080 KB
最終ジャッジ日時 2023-09-07 06:59:38
合計ジャッジ時間 6,524 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,184 KB
testcase_01 AC 99 ms
76,492 KB
testcase_02 AC 78 ms
75,544 KB
testcase_03 AC 105 ms
76,488 KB
testcase_04 AC 82 ms
75,572 KB
testcase_05 AC 80 ms
75,568 KB
testcase_06 AC 98 ms
76,576 KB
testcase_07 AC 97 ms
76,636 KB
testcase_08 AC 94 ms
76,392 KB
testcase_09 AC 95 ms
76,372 KB
testcase_10 AC 93 ms
76,504 KB
testcase_11 AC 419 ms
98,080 KB
testcase_12 AC 373 ms
95,944 KB
testcase_13 AC 383 ms
95,628 KB
testcase_14 AC 414 ms
97,968 KB
testcase_15 AC 344 ms
92,312 KB
testcase_16 AC 417 ms
97,496 KB
testcase_17 AC 406 ms
97,336 KB
testcase_18 AC 419 ms
97,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

# C[k] : 1 (B[k] != 0) 0 (B[k] == 0)

# N: 処理する区間の長さ
N0 = 2**(M-1).bit_length()
INF = 2**31-1
data = [0]*(2*N0)

# c_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = data[2*k+1] + data[2*k+2]

# 区間[l, r)の総和
def query(l, r):
    L = l + N0; R = r + N0
    s = 0
    while L < R:
        if R & 1:
            R -= 1
            s += data[R-1]
        if L & 1:
            s += data[L-1]
            L += 1
        L >>= 1; R >>= 1
    return s

# 配列の初期化
for i, b in enumerate(B):
    update(i, b != 0)

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