結果

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

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    N, M = map(int, sys.stdin.readline().split())
    operations = []
    s = set()
    for _ in range(M):
        B, C = map(int, sys.stdin.readline().split())
        if C > B:
            operations.append((B, C))
            s.add(B)
            s.add(C)
    
    # No valid operations
    if not s:
        print(N * (N + 1) // 2)
        return
    
    sorted_v = sorted(s, reverse=True)
    b_to_cs = defaultdict(list)
    for B, C in operations:
        b_to_cs[B].append(C)
    
    max_val = {}
    for v in sorted_v:
        current_max = v
        for C in b_to_cs.get(v, []):
            c_max = max_val.get(C, C)
            if c_max > current_max:
                current_max = c_max
        max_val[v] = current_max
    
    total = N * (N + 1) // 2
    for v in s:
        total += (max_val[v] - v)
    print(total)

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