Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_right
N,Q,*L = map(int, open(0).read().split())
D = L[3*N:]
ls = [0]*N
for i in range(N):
  s,t,x = L[3*i],L[3*i+1],L[3*i+2]
  ls[i] = (x,s,t)
ls.sort()
line = [0]*(2*N)
for i in range(N):
  x,s,t = ls[i]
  line[2*i] = (s-x,0,i)
  line[2*i+1] = (t-x,1,i)
line.sort()
road = [c[0] for c in line]
inf = [0]*(2*N)
m = 0
for i in range(2*N):
  d,p,j = line[i]
  if p:
    m -= 1<<j
  else:
    m += 1<<j
  inf[i] = m
for d in D:
  i = bisect_right(road,d)
  if i==2*N or i==0:
    print(-1)
    continue
  m = inf[i-1]
  s = m&-m
  if s==0:
    print(-1)
  else:
    ind = len(bin(s)[2:])-1
    print(ls[ind][0])

Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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 python submission (verdict: Time Limit Exceeded):
from bisect import bisect_right
N,Q,*L = map(int, open(0).read().split())
D = L[3*N:]
ls = [0]*N
for i in range(N):
  s,t,x = L[3*i],L[3*i+1],L[3*i+2]
  ls[i] = (x,s,t)
ls.sort()
line = [0]*(2*N)
road = [0]*(2*N)
for i in range(N):
  x,s,t = ls[i]
  line[2*i] = (s-x,0,i)
  line[2*i+1] = (t-x,1,i)
  road[2*i] = s-x
  road[2*i+1] = t-x
line.sort()
road.sort()
inf = [0]*(2*N)
m = 0
for i in range(2*N):
  d,p,j = line[i]
  if p:
    m -= 1<<j
  else:
    m += 1<<j
  inf[i] = m
for d in D:
  i = bisect_right(road,d)
  if i==2*N or i==0:
    print(-1)
    continue
  m = inf[i-1]
  s = m&-m
  if s==0:
    print(-1)
  else:
    ind = len(bin(s)[2:])-1
    print(ls[ind][0])

Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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 python submission (verdict: Time Limit Exceeded):
from bisect import bisect_right
N,Q,*L = map(int, open(0).read().split())
D = L[3*N:]
ls = [0]*N
for i in range(N):
  s,t,x = L[3*i],L[3*i+1],L[3*i+2]
  ls[i] = (x,s,t)
ls.sort()
line = [0]*(2*N)
road = [0]*(2*N)
for i in range(N):
  x,s,t = ls[i]
  line[2*i] = (s-x,0,i)
  line[2*i+1] = (t-x,1,i)
  road[2*i] = s-x
  road[2*i+1] = t-x
line.sort()
road.sort()
inf = [0]*(2*N)
m = 0
for i in range(2*N):
  d,p,j = line[i]
  if p:
    m -= 1<<j
  else:
    m += 1<<j
  inf[i] = m
for d in D:
  i = bisect_right(road,d)
  if i==2*N or i==0:
    print(-1)
    continue
  m = inf[i-1]
  s = m&-m
  if s==0:
    print(-1)
  else:
    ind = len(bin(s)[2:])-1
    print(ls[ind][0])

Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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 python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop
N, Q = map(int, input().split())
ls = []
for i in range(N):
  s, t, x = map(int, input().split())
  ls += [(s-x,1,x)]
  ls += [(t-x,0,x)]
for i in range(Q):
  d = int(input())
  ls += [(d,2,i)]
ls.sort()
se = set()
q = []
for a,b,c in ls:
  if b==1:
    se.add(c)
    heappush(q,c)
  elif b==0:
    se.remove(c)
  else:
    while q and q[0] not in se:
      heappop(q)
    ans = q[0] if q else -1
    print(ans)


Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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 python submission (verdict: Time Limit Exceeded):
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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)))


Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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 python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop
N, Q = map(int, input().split())
ls = []
for i in range(N):
  s, t, x = map(int, input().split())
  ls += [(s-x,1,x)]
  ls += [(t-x,0,x)]
for i in range(Q):
  d = int(input())
  ls += [(d,2,i)]
ls.sort()
se = set()
q = []
for a,b,c in ls:
  if b==1:
    se.add(c)
    heappush(q,c)
  elif b==0:
    se.remove(c)
  else:
    while q and q[0] not in se:
      heappop(q)
    ans = q[0] if q else -1
    print(ans)


Fixed submission:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline
from heapq import heappop,heappush
from collections import defaultdict

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)) # xで止まらなくなる
  task.append((s-x,1,x)) # xで止まる

for i in range(Q):
  d = int(input())
  task.append((d,2,i)) # 止まる位置を答える
answer = [-1]*Q

task.sort()

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

# 小さい時刻から順に見ていく
for a,b,c in task:
  if not b: # b == 0
    se.remove(c)
  elif b&1: # b == 1
    se.add(c)
    heappush(se_hp,c)
  else: # b == 2
    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)))