Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())

lst = []
for i in range(N):
    S, T , X = map(int, input().split())
    lst += [[X, max(0, S-X), max(0, T-X)]]

lst.sort()
#print (lst)

def check(a):
    for j in range(len(lst)):
            if lst[j][1] <= a and a < lst[j][2]:
                return lst[j][0]
            elif j == len(lst) - 1:
                return -1

for i in range(Q):
    D = int(input())
    print (check(D))

Fixed submission:
import sys
input = sys.stdin.readline

from heapq import heappop, heappush
from collections import defaultdict

N, Q = map(int, input().split())

lst = []
for i in range(N):
    s, t, x = map(int, input().split())
    lst.append((t - x, 0, x)) #xで止まらなくなる
    lst.append((s - x, 1, x)) #xで止まる

for i in range(Q):
    lst.append((int(input()), 2, i))

lst.sort()
ans = [-1] * Q

#引っかかっている場所の管理
se = set()
se_hp = [] #heapで最小値を先頭に保つ

#小さい時刻から順に見ていく
for a, b, c in lst:
    if b == 0:
        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)
        ans[c] = se_hp[0] if se_hp else -1

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

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())

lst = []
for i in range(N):
    S, T , X = map(int, input().split())
    lst += [[X, max(0, S-X), max(0, T-X)]]

lst.sort()
#print (lst)

def check(a):
    for j in range(len(lst)):
            if lst[j][1] <= a and a < lst[j][2]:
                return lst[j][0]
            elif j == len(lst) - 1:
                return -1

for i in range(Q):
    D = int(input())
    print (check(D))

Fixed submission:
import sys
input = sys.stdin.readline

from heapq import heappop, heappush
from collections import defaultdict

N, Q = map(int, input().split())

lst = []
for i in range(N):
    s, t, x = map(int, input().split())
    lst.append((t - x, 0, x)) #xで止まらなくなる
    lst.append((s - x, 1, x)) #xで止まる

for i in range(Q):
    lst.append((int(input()), 2, i))

lst.sort()
ans = [-1] * Q

#引っかかっている場所の管理
se = set()
se_hp = [] #heapで最小値を先頭に保つ

#小さい時刻から順に見ていく
for a, b, c in lst:
    if b == 0:
        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)
        ans[c] = se_hp[0] if se_hp else -1

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

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())

lst = []
for i in range(N):
    S, T , X = map(int, input().split())
    lst += [[X, max(0, S-X), max(0, T-X)]]

lst.sort()
#print (lst)

def check(a):
    for j in range(len(lst)):
            if lst[j][1] <= D and D < lst[j][2]:
                return lst[j][0]
            elif j == len(lst) - 1:
                return -1

for i in range(Q):
    D = int(input())
    print (check(D))

Fixed submission:
import sys
input = sys.stdin.readline

from heapq import heappop, heappush
from collections import defaultdict

N, Q = map(int, input().split())

lst = []
for i in range(N):
    s, t, x = map(int, input().split())
    lst.append((t - x, 0, x)) #xで止まらなくなる
    lst.append((s - x, 1, x)) #xで止まる

for i in range(Q):
    lst.append((int(input()), 2, i))

lst.sort()
ans = [-1] * Q

#引っかかっている場所の管理
se = set()
se_hp = [] #heapで最小値を先頭に保つ

#小さい時刻から順に見ていく
for a, b, c in lst:
    if b == 0:
        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)
        ans[c] = se_hp[0] if se_hp else -1

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

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())

lst = []
for i in range(N):
    S, T , X = map(int, input().split())
    lst += [[X, max(0, S-X), max(0, T-X)]]

lst.sort()
#print (lst)

def check(a):
    for j in range(len(lst)):
            if lst[j][1] <= D and D < lst[j][2]:
                return lst[j][0]
            elif j == len(lst) - 1:
                return -1

for i in range(Q):
    D = int(input())
    print (check(D))

Fixed submission:
import sys
input = sys.stdin.readline

from heapq import heappop, heappush
from collections import defaultdict

N, Q = map(int, input().split())

lst = []
for i in range(N):
    s, t, x = map(int, input().split())
    lst.append((t - x, 0, x)) #xで止まらなくなる
    lst.append((s - x, 1, x)) #xで止まる

for i in range(Q):
    lst.append((int(input()), 2, i))

lst.sort()
ans = [-1] * Q

#引っかかっている場所の管理
se = set()
se_hp = [] #heapで最小値を先頭に保つ

#小さい時刻から順に見ていく
for a, b, c in lst:
    if b == 0:
        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)
        ans[c] = se_hp[0] if se_hp else -1

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