Buggy python submission (verdict: Time Limit Exceeded):
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))
    ls.sort(reverse=True)
    lt.sort(reverse=True)
    dh = []
    dl = []
    for i in range(Q):
        d = int(input())
        while ls and ls[-1][0] <= d:
            s, x = ls.pop()
            heapq.heappush(dl, x)
        while lt and lt[-1][0] <= d:
                t, x = lt.pop()
                heapq.heappush(dh, x)
        while dl and dh and dl[0] == dh[0]:
            heapq.heappop(dl)
            heapq.heappop(dh)
        if not dl:
            print('-1')
        else:
            print(dl[0])
main()


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 python submission (verdict: Time Limit Exceeded):
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))
    ls.sort(reverse=True, key=lambda x: x[0])
    lt.sort(reverse=True, key=lambda x: x[0])
    dh = []
    dl = []
    heapq.heapify(dh)
    heapq.heapify(dl)
    for i in range(Q):
        d = int(input())
        while len(ls) > 0 and ls[-1][0] <= d:
            s, x = ls.pop()
            heapq.heappush(dl, x)
        while len(lt) > 0 and lt[-1][0] <= d:
                t, x = lt.pop()
                heapq.heappush(dh, x)
        while len(dl) > 0 and len(dh) > 0 and dl[0] == dh[0]:
            heapq.heappop(dl)
            heapq.heappop(dh)
        if not dl:
            print('-1')
        else:
            print(dl[0])
main()


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 python submission (verdict: Time Limit Exceeded):
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))
    ls.sort(reverse=True, key=lambda x: x[0])
    lt.sort(reverse=True, key=lambda x: x[0])
    dh = []
    dl = []
    heapq.heapify(dh)
    heapq.heapify(dl)
    for i in range(Q):
        d = int(input())
        while len(ls) > 0 and ls[-1][0] <= d:
            s, x = ls.pop()
            heapq.heappush(dl, x)
        while len(lt) > 0 and lt[-1][0] <= d:
                t, x = lt.pop()
                heapq.heappush(dh, x)
        while len(dl) > 0 and len(dh) > 0 and dl[0] == dh[0]:
            heapq.heappop(dl)
            heapq.heappop(dh)
        if not dl:
            print('-1')
        else:
            print(dl[0])
main()


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 python 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()


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 python 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()


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 python 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()


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()