結果
問題 | No.2220 Range Insert & Point Mex |
ユーザー |
![]() |
提出日時 | 2025-06-12 21:25:36 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,450 bytes |
コンパイル時間 | 237 ms |
コンパイル使用メモリ | 81,848 KB |
実行使用メモリ | 192,368 KB |
最終ジャッジ日時 | 2025-06-12 21:26:29 |
合計ジャッジ時間 | 12,751 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 TLE * 1 |
ソースコード
import sys from collections import defaultdict def main(): input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 a_set = set() intervals = defaultdict(list) for _ in range(N): l = int(input[ptr]) r = int(input[ptr+1]) a = int(input[ptr+2]) ptr +=3 a_set.add(a) intervals[a].append( (l, r) ) # Compute M M = 0 while M in a_set: M +=1 if M == 0: Q = int(input[ptr]) ptr +=1 ptr += Q for _ in range(Q): print(0) return # Merge intervals for each a merged = defaultdict(list) for a in intervals: sorted_intervals = sorted(intervals[a]) if not sorted_intervals: continue current_l, current_r = sorted_intervals[0] for l, r in sorted_intervals[1:]: if l <= current_r +1: current_r = max(current_r, r) else: merged[a].append( (current_l, current_r) ) current_l, current_r = l, r merged[a].append( (current_l, current_r) ) # Generate events events = [] for a in merged: for l, r in merged[a]: events.append( (l, 'add', a) ) events.append( (r+1, 'remove', a) ) # Sort events: by x, then 'remove' before 'add' events.sort(key=lambda x: (x[0], 0 if x[1] == 'remove' else 1)) # Read queries Q = int(input[ptr]) ptr +=1 queries = list(map(int, input[ptr:ptr+Q])) ptr += Q # Process events and queries event_ptr = 0 freq = defaultdict(int) active_set = set() current_mex = 0 for x in queries: while event_ptr < len(events) and events[event_ptr][0] <= x: x_event, type_event, a = events[event_ptr] if type_event == 'add': freq[a] +=1 if freq[a] == 1: active_set.add(a) if a == current_mex: while current_mex in active_set: current_mex +=1 else: if freq[a] >0: freq[a] -=1 if freq[a] ==0: active_set.discard(a) if a < current_mex: current_mex = min(current_mex, a) event_ptr +=1 print(current_mex) if __name__ == "__main__": main()