結果

問題 No.1194 Replace
ユーザー lam6er
提出日時 2025-04-16 15:46:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 929 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 132,276 KB
最終ジャッジ日時 2025-04-16 15:47:45
合計ジャッジ時間 5,280 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    
    max_map = {}
    
    for _ in range(M):
        B = int(data[idx])
        idx += 1
        C = int(data[idx])
        idx += 1
        if B < C:
            if B in max_map:
                if C > max_map[B]:
                    max_map[B] = C
            else:
                max_map[B] = C
    
    memo = {}
    
    def compute_max(x):
        if x not in max_map:
            return x
        if x in memo:
            return memo[x]
        next_x = max_map[x]
        m = compute_max(next_x)
        memo[x] = m
        return m
    
    sum_initial = N * (N + 1) // 2
    sum_diff = 0
    
    for x in max_map:
        m = compute_max(x)
        sum_diff += (m - x)
    
    print(sum_initial + sum_diff)

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