Problem p03033 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from heapq import heappop,heappush

N,Q = map(int,input().split())
task = []

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  task.append((t-x,0,x))
  task.append((s-x,1,x))

for i in range(Q):
  d = int(input())
  task.append((d,2,i))
answer = [-1]*Q

task.sort()

se = set()
se_hp = []
for a,b,c in task:
  if not b:
    se.remove(c)
  elif b&1:
    se.add(c)
    heappush(se_hp,c)
  else:
    while se_hp and se_hp[0] not in se:
      heappop(se_hp)
    answer[c] = se_hp[0] if se_hp else -1

print('\n'.join(map(str,answer)))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter
import heapq

def main():
    N,Q = map(int,input().split())
    event = []
    
    for _ in range(N):
        s,t,x = map(int,input().split())
        event.append((s-x,0,x))
        event.append((t-x,1,x))
        
    for i in range(Q):
        a = int(input())
        event.append((a,2,i))
    
    event.sort(key=itemgetter(0))
    ans = [-1]*Q
    use = set()
    stop = []
    heapq.heapify(stop)
    
    for a,b,c in event:
        if b == 0:
            use.add(c)
            heapq.heappush(stop,c)
        elif b == 1:
            use.remove(c)
        else:
            while stop and stop[0] not in use:
                heapq.heappop(stop)
            ans[c] = -1 if len(stop) == 0 else stop[0]
    
    for num in ans:
        print(num)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter
import heapq

def main():
    N,Q = map(int,input().split())
    event = []
    
    for _ in range(N):
        s,t,x = map(int,input().split())
        event.append((s-x,0,x))
        event.append((t-x,1,x))
        
    for i in range(Q):
        a = int(input())
        event.append((a,2,-1))
    
    event.sort(key=itemgetter(0))
    
    use = set()
    stop = []
    heapq.heapify(stop)
    
    for a,b,c in event:
        if b == 0:
            use.add(c)
            heapq.heappush(stop,c)
        elif b == 1:
            use.remove(c)
        else:
            while stop and stop[0] not in use:
                heapq.heappop(stop)
            if stop == []:
                print(-1)
            else:
                print(stop[0])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter
import heapq

def main():
    N,Q = map(int,input().split())
    event = []
    
    for _ in range(N):
        s,t,x = map(int,input().split())
        event.append((s-x,0,x))
        event.append((t-x,1,x))
        
    for i in range(Q):
        a = int(input())
        event.append((a,2,-1))
    
    event.sort(key=itemgetter(0))
    
    use = set()
    stop = []
    heapq.heapify(stop)
    
    for a,b,c in event:
        if b == 0:
            use.add(c)
            heapq.heappush(stop,c)
        elif b == 1:
            use.remove(c)
        else:
            while stop and stop[0] not in use:
                heapq.heappop(stop)
            if stop == []:
                print(-1)
            else:
                print(stop[0])

if __name__ == "__main__":
    main()