結果

問題 No.2321 Continuous Flip
ユーザー qwewe
提出日時 2025-05-14 12:52:08
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,235 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 52,608 KB
最終ジャッジ日時 2025-05-14 12:54:00
合計ジャッジ時間 5,385 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other TLE * 1 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N, M, C = map(int, input[ptr:ptr+3])
    ptr +=3
    A = list(map(int, input[ptr:ptr+N]))
    ptr +=N
    ops = []
    sum_A = []
    for _ in range(M):
        L = int(input[ptr])-1  # 0-based
        R = int(input[ptr+1])-1
        ptr +=2
        ops.append((L, R))
        s = sum(A[L:R+1])
        sum_A.append(s)
    
    # Initial gain is sum_A[j] - C
    heap = []
    for j in range(M):
        gain = sum_A[j] - C
        if gain > 0:
            heapq.heappush(heap, (-gain, j))
    
    selected = [False]*M
    parities = [0]*N  # 0: even, 1: odd
    total = 0
    X = 0
    
    while heap:
        neg_gain, j = heapq.heappop(heap)
        gain = -neg_gain
        if selected[j]:
            continue
        current_gain = sum_A[j] - C - 2 * sum(A[i] * parities[i] for i in range(ops[j][0], ops[j][1]+1))
        if current_gain <=0:
            continue
        # Select this operation
        total += current_gain
        X +=1
        selected[j] = True
        # Flip the parities
        L, R = ops[j]
        for i in range(L, R+1):
            parities[i] ^=1
    
    print(total)

if __name__ == '__main__':
    main()
0