結果

問題 No.2220 Range Insert & Point Mex
ユーザー lam6er
提出日時 2025-04-16 16:02:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,688 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 173,144 KB
最終ジャッジ日時 2025-04-16 16:08:21
合計ジャッジ時間 20,452 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1

    events = []
    for _ in range(N):
        l = int(input[ptr])
        r = int(input[ptr+1])
        a = int(input[ptr+2])
        ptr +=3
        # Add event
        events.append( (l, 1, a) )
        # Remove event (at r+1)
        events.append( (r+1, 0, a) )
    
    # Sort events: first by x, then remove events (0) come before add (1)
    events.sort(key=lambda x: (x[0], x[1]))
    
    Q = int(input[ptr])
    ptr +=1
    queries = list(map(int, input[ptr:ptr+Q]))
    ptr += Q
    
    event_ptr = 0
    current_mex = 0
    counts = defaultdict(int)
    active = set()
    answers = []
    
    for x in queries:
        while event_ptr < len(events) and events[event_ptr][0] <= x:
            ex, etype, ea = events[event_ptr]
            if etype == 1:
                # Add event
                counts[ea] +=1
                if counts[ea] == 1:
                    active.add(ea)
                    if ea == current_mex:
                        # Increment mex until not found
                        while current_mex in active:
                            current_mex +=1
            else:
                # Remove event
                counts[ea] -=1
                if counts[ea] == 0:
                    if ea in active:
                        active.remove(ea)
                    if ea < current_mex:
                        current_mex = min(current_mex, ea)
            event_ptr +=1
        answers.append(current_mex)
    
    for ans in answers:
        print(ans)
    
if __name__ == "__main__":
    main()
0