Buggy python submission (verdict: Time Limit Exceeded):
class BIT():
	def __init__(self, n):
		self.n = n
		self.bit = [0] * (n + 1)

	def getsum(self, k):
		ret = 0
		while k:
			ret += self.bit[k]
			k -= k & -k
		return ret

	def p_sum(self, i, j):
		return self.getsum(j) - self.getsum(i - 1)

	def add(self, k, x):
		while k <= self.n:
			self.bit[k] += x
			k += k & -k

	def bisect(self, x):
		i, m = 0, 1 << (self.n.bit_length() - 1)
		while m:
			t = i + m
			if t < self.n and self.bit[t] < x:
				i = t
				x -= self.bit[i]
			m //= 2
		return i + 1

import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
def main():
	n, q = map(int, input().split())
	event = []
	Xs = []
	for _ in range(n):
		s, t, x = map(int, input().split())
		event.append((-2, s - x, x))
		event.append((-1, t - x, x))
		Xs.append(x)
	Xs.sort()
	for _ in range(q):
		d = int(input())
		event.append((0, d, None))
	event.sort(key=get(1))
	x_to_i = {x: i for i, x in enumerate(Xs)}
	bit = BIT(n)
	any = 0
	ans = []
	for flg, time, x in event:
		if flg == -2:
			bit.add(x_to_i[x], 1)
			any += 1
		elif flg == -1:
			bit.add(x_to_i[x], -1)
			any -= 1
		else:
			if not any:
				ans.append(-1)
			else:
				ans.append(Xs[bit.bisect(1)])
	print("\n".join(map(str, ans)))

if __name__ == '__main__':
	main()

Fixed submission:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
from heapq import *


def main():
	n, q = map(int, input().split())
	event = []
	Xs = []
	for _ in range(n):
		s, t, x = map(int, input().split())
		event.append((-2, s - x, x))
		event.append((-1, t - x, x))
		Xs.append(x)
	for _ in range(q):
		d = int(input())
		event.append((0, d, None))
	event.sort(key=get(1))
	active = {x: 0 for x in Xs}
	candidate = []
	ans = []
	for flg, time, x in event:
		if flg == -2:
			active[x] = 1
			heappush(candidate, x)
		elif flg == -1:
			active[x] = 0
		else:
			res = -1
			while candidate:
				c = heappop(candidate)
				if active[c]:
					heappush(candidate, c)
					res = c
					break
			ans.append(res)
	print("\n".join(map(str, ans)))


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
class BIT():
	def __init__(self, n):
		self.n = n
		self.bit = [0] * (n + 1)

	def getsum(self, k):
		ret = 0
		while k:
			ret += self.bit[k]
			k -= k & -k
		return ret

	def p_sum(self, i, j):
		return self.getsum(j) - self.getsum(i - 1)

	def add(self, k, x):
		while k <= self.n:
			self.bit[k] += x
			k += k & -k

	def bisect(self, x):
		i, m = 0, 1 << (self.n.bit_length() - 1)
		while m:
			t = i + m
			if t < self.n and self.bit[t] < x:
				i = t
				x -= self.bit[i]
			m //= 2
		return i + 1

import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
n,q=map(int,input().split())
event=[]
Xs=[]
for _ in range(n):
	s,t,x=map(int,input().split())
	event.append((-2,s-x,x))
	event.append((-1,t-x,x))
	Xs.append(x)
Xs.sort()
for _ in range(q):
	d=int(input())
	event.append((0,d,None))
event.sort(key=get(1))
x_to_i={x:i for i,x in enumerate(Xs)}
bit=BIT(n+1)
Xs.append(-1)
bit.add(n,1)
ans=[]
for flg,time,x in event:
	if flg==-2:
		bit.add(x_to_i[x],1)
	elif flg==-1:
		bit.add(x_to_i[x],-1)
	else:
		i=bit.bisect(1)
		ans.append(Xs[i])
print("\n".join(map(str,ans)))




Fixed submission:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
from heapq import *


def main():
	n, q = map(int, input().split())
	event = []
	Xs = []
	for _ in range(n):
		s, t, x = map(int, input().split())
		event.append((-2, s - x, x))
		event.append((-1, t - x, x))
		Xs.append(x)
	for _ in range(q):
		d = int(input())
		event.append((0, d, None))
	event.sort(key=get(1))
	active = {x: 0 for x in Xs}
	candidate = []
	ans = []
	for flg, time, x in event:
		if flg == -2:
			active[x] = 1
			heappush(candidate, x)
		elif flg == -1:
			active[x] = 0
		else:
			res = -1
			while candidate:
				c = heappop(candidate)
				if active[c]:
					heappush(candidate, c)
					res = c
					break
			ans.append(res)
	print("\n".join(map(str, ans)))


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
class BIT():
	def __init__(self, n):
		self.n = n
		self.bit = [0] * (n + 1)

	def getsum(self, k):
		ret = 0
		while k:
			ret += self.bit[k]
			k -= k & -k
		return ret

	def p_sum(self, i, j):
		return self.getsum(j) - self.getsum(i - 1)

	def add(self, k, x):
		while k <= self.n:
			self.bit[k] += x
			k += k & -k

	def bisect(self, x):
		i, m = 0, 1 << (self.n.bit_length() - 1)
		while m:
			t = i + m
			if t < self.n and self.bit[t] < x:
				i = t
				x -= self.bit[i]
			m //= 2
		return i + 1

import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
n,q=map(int,input().split())
event=[]
Xs=[]
for _ in range(n):
	s,t,x=map(int,input().split())
	event.append((-2,s-x,x))
	event.append((-1,t-x,x))
	Xs.append(x)
Xs.sort()
for _ in range(q):
	d=int(input())
	event.append((0,d,None))
event.sort(key=get(1))
x_to_i={x:i for i,x in enumerate(Xs)}
bit=BIT(n+1)
Xs.append(-1)
bit.add(n,1)
ans=[]
for flg,time,x in event:
	if flg==-2:
		bit.add(x_to_i[x],1)
	elif flg==-1:
		bit.add(x_to_i[x],-1)
	else:
		i=bit.bisect(1)
		ans.append(Xs[i])
print("\n".join(map(str,ans)))

Fixed submission:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter as get
from heapq import *


def main():
	n, q = map(int, input().split())
	event = []
	Xs = []
	for _ in range(n):
		s, t, x = map(int, input().split())
		event.append((-2, s - x, x))
		event.append((-1, t - x, x))
		Xs.append(x)
	for _ in range(q):
		d = int(input())
		event.append((0, d, None))
	event.sort(key=get(1))
	active = {x: 0 for x in Xs}
	candidate = []
	ans = []
	for flg, time, x in event:
		if flg == -2:
			active[x] = 1
			heappush(candidate, x)
		elif flg == -1:
			active[x] = 0
		else:
			res = -1
			while candidate:
				c = heappop(candidate)
				if active[c]:
					heappush(candidate, c)
					res = c
					break
			ans.append(res)
	print("\n".join(map(str, ans)))


if __name__ == '__main__':
	main()