結果

問題 No.876 Range Compress Query
ユーザー pynomipynomi
提出日時 2019-09-07 01:51:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,130 bytes
コンパイル時間 848 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 97,760 KB
最終ジャッジ日時 2023-09-07 06:36:37
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,440 KB
testcase_01 WA -
testcase_02 AC 80 ms
75,636 KB
testcase_03 WA -
testcase_04 AC 85 ms
75,600 KB
testcase_05 AC 82 ms
75,424 KB
testcase_06 AC 103 ms
76,340 KB
testcase_07 WA -
testcase_08 AC 102 ms
76,484 KB
testcase_09 WA -
testcase_10 AC 98 ms
76,364 KB
testcase_11 AC 441 ms
97,216 KB
testcase_12 AC 385 ms
93,864 KB
testcase_13 AC 387 ms
94,416 KB
testcase_14 AC 429 ms
97,396 KB
testcase_15 AC 354 ms
93,040 KB
testcase_16 AC 419 ms
97,132 KB
testcase_17 AC 420 ms
96,520 KB
testcase_18 AC 442 ms
97,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, Q = map(int,input().split())
A = list(map(int,input().split()))
queries = [[int(e)-1 for e in 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
        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
        ans = query(l, r) + 1
        print(ans)
0