Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop
n, q = map(int, input().split())
events = []
for i in range(n):
    s, t, i = map(int, input().split())
    if t >= i:
        events.append([t - i, 0, i])
        events.append([s - i, 1, i])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()
heap = []
working = set()
distance = [-1] * q
for t, kind, i in events:
    if kind == 0:
        working.remove(i)
    elif kind == 1:
        working.add(i)
        heappush(heap, i)
    elif working:
        while not heap[0] in working:
            heappop(heap)
        distance[i] = heap[0]
for d in distance:
    print(d)

Fixed submission:
from heapq import heappush, heappop


n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = set()
heap = []
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        working.add(x)
        heappush(heap, x)
    elif working:
        while not heap[0] in working:
            heappop(heap)
        dist[x] = heap[0]

for d in dist:
    print(d)

Buggy python submission (verdict: Time Limit Exceeded):
def sl_insert(vals:list, new_val):
    index = sl_indexof(vals, new_val)
    if index < 0:
        index = -index
    vals.insert(index, new_val)   
    return
def sl_remove(vals:list, new_val):
    index = sl_indexof(vals, new_val)
    if index < 0:
        return
    del vals[index]
    return

def sl_indexof(vals:list, new_val):
    l = 0
    r = len(vals)
    while l < r:
        m = (l + r) // 2
        if vals[m] == new_val:
            return m
        elif vals[m] < new_val:
            l = m + 1
        else:
            r = m
    return -l

n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = []
for t, kind, x in events:
    if kind == 0:
        sl_remove(working, x)
    elif kind == 1:
        sl_insert(working, x)
    elif any(working):
        dist[x] = working[0]
    else:
        walker.append(x)

for d in dist:
    print(d)

Fixed submission:
from heapq import heappush, heappop


n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = set()
heap = []
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        working.add(x)
        heappush(heap, x)
    elif working:
        while not heap[0] in working:
            heappop(heap)
        dist[x] = heap[0]

for d in dist:
    print(d)

Buggy python submission (verdict: Time Limit Exceeded):
def sl_insert(vals:list, new_val):
    l = 0
    r = len(vals)
    while l < r:
        m = (l + r) // 2
        if vals[m] == new_val:
            vals.insert(m, new_val)
            return
        elif vals[m] < new_val:
            l = m + 1
        else:
            r = m
    vals.insert(l, new_val)
    return

n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = []
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        sl_insert(working, x)
    elif any(working):
        dist[x] = working[0]
    else:
        walker.append(x)

for d in dist:
    print(d)

Fixed submission:
from heapq import heappush, heappop


n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = set()
heap = []
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        working.add(x)
        heappush(heap, x)
    elif working:
        while not heap[0] in working:
            heappop(heap)
        dist[x] = heap[0]

for d in dist:
    print(d)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush
n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = set()
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        working.add(x)
    elif any(working):
        dist[x] = min(working)
    else:
        walker.append(x)

for d in dist:
    print(str(d))

Fixed submission:
from heapq import heappush, heappop


n, q = map(int, input().split())
events = []

for i in range(n):
    s, t, x = map(int, input().split())
    if t >= x:
        events.append([t - x, 0, x])
        events.append([s - x, 1, x])
for i in range(q):
    d = int(input())
    events.append([d, 2, i])
events.sort()

dist = [-1] * q
walker = []
working = set()
heap = []
for t, kind, x in events:
    if kind == 0:
        working.remove(x)
    elif kind == 1:
        working.add(x)
        heappush(heap, x)
    elif working:
        while not heap[0] in working:
            heappop(heap)
        dist[x] = heap[0]

for d in dist:
    print(d)