Problem p03033 (python)

Fixed submission:
import heapq
def main():
    N, Q = map(int, input().split())
    l = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        l.append((s-x, 1, x))
        l.append((t-x, 0, x))
    for i in range(Q):
        d = int(input())
        l.append((d, 2, i))
    r = [-1] * Q
    l.sort()
    h = []
    se = set()
    for a, b, c in l:
        if b == 0:
            se.remove(c)
        elif b == 1:
            se.add(c)
            heapq.heappush(h, c)
        else:
            while h and h[0] not in se:
                heapq.heappop(h)
            r[c] = h[0] if h else -1
    print('\n'.join(map(str, r)))
            
main()

Buggy submission (verdict: Runtime Error):
import heapq
def main():
    N, Q = map(int, input().split())
    ls = []
    lt = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        ls.append((s-x, x))
        lt.append((t-x, x))
    heapq.heapify(ls)
    heapq.heapify(lt)
    dl = set()
    m = 0
    for i in range(Q):
        d = int(input())
        if len(ls) > 0 and ls[0][0] > d:
            while len(lt) > 0 and lt[0][0] <= d:
                t, x = heapq.heappop(lt)
                dl.remove(x)
        elif len(ls) > 0:
            while len(lt) > 0 and lt[0][0] <= ls[0][0]:
                t, x = heapq.heappop(lt)
                dl.remove(x)
            while len(ls) > 0 and ls[0][0] <= d:
                s, x = heapq.heappop(ls)
                dl.add(x)
                while len(lt) > 0 and lt[0][0] <= s:
                    t, x = heapq.heappop(lt)
                    dl.remove(x)
            while len(lt) > 0 and lt[0][0] <= d:
                t, x = heapq.heappop(lt)
                dl.remove(x)
        else:
            while len(lt) > 0 and lt[0][0] <= d:
                t, x = heapq.heappop(lt)
                dl.remove(x)
        if not dl:
            print('-1')
        else:
            if m not in dl:
                m = min(dl)
            print(min(dl))
main()

Buggy submission (verdict: Runtime Error):
import heapq
def main():
    N, Q = map(int, input().split())
    ls = []
    lt = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        ls.append((s-x, x))
        lt.append((t-x, x))
    heapq.heapify(ls)
    heapq.heapify(lt)
    dl = set()
    for i in range(Q):
        d = int(input())
        while len(lt) > 0 and lt[0][0] <= d:
            t, x = heapq.heappop(lt)
            dl.remove(x)
        while len(ls) > 0 and ls[0][0] <= d:
            s, x = heapq.heappop(ls)
            dl.add(x)
        if not dl:
            print('-1')
        else:
            print(min(dl))
main()

Buggy submission (verdict: Runtime Error):
import heapq
def main():
    N, Q = map(int, input().split())
    ls = []
    lt = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        ls.append((s-x, x))
        lt.append((t-x, x))
    heapq.heapify(ls)
    heapq.heapify(lt)
    dl = set()
    for i in range(Q):
        d = int(input())
        while len(lt) > 0 and lt[0][0] <= d:
            t, x = heapq.heappop(lt)
            dl.remove(x)
        while len(ls) > 0 and ls[0][0] <= d:
            s, x = heapq.heappop(ls)
            dl.add(x)
        if not dl:
            print('-1')
        else:
            print(min(dl))
main()