Problem p03033 (python)

Fixed submission:
import sys
from operator import itemgetter
def main():
	inputs = sys.stdin.readline
	N, Q= map(int,inputs().split())
	E = [None]*2*N
	for n in range(N):
		a,b,c = map(int,inputs().split())
		E[2*n] = (a - c, 1, c)
		E[2*n + 1] = (b - c, -1, c)
	E.sort(key=itemgetter(0))
	tuple(tuple(E))
	mini = 1000000001
	kouho = {1000000001}
	my_add = kouho.add
	my_discard = kouho.discard
	q = 0
	d = int(inputs())
	flag = False
	cont = False
	for e in E:
		while e[0] > d:
			if cont == True:
				print(mini-(mini//1000000001)*1000000002)
			else:
				mini = min(kouho)
				print(mini-(mini//1000000001)*1000000002)
			cont = True
			q = q + 1
			if q > Q-1:
				flag = True
				break
			d = int(inputs())
		if flag == True:
			break
		if e[1] == 1:
			my_add(e[2])
			if mini > e[2]:
				mini = e[2]
		elif e[1] == -1:
			my_discard(e[2])
			if mini == e[2]:
				cont = False
	for t in [None]*(Q-q):
		print(-1)
main()

Buggy submission (verdict: Runtime Error):
import sys
from operator import itemgetter
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
E = [None]*2*N
for n in range(N):
	a,b,c = map(int,inputs().split())
	E[2*n] = (a - c, 1, c)
	E[2*n + 1] = (b - c, -1, c)
E.sort(key=itemgetter(0))
tuple(E)
mini = 1000000001
kouho = {1000000001}
my_add = kouho.add
my_discard = kouho.remove
q = 0
d = int(inputs())
flag = False
cont = False
for e in E:
	while(e[0] > d):
		if cont == True:
			if mini < 1000000001:
				print(mini)
			else:
				print(-1)
		else:
			mini = min(kouho)
			if mini < 1000000001:
				print(mini)
			else:
				print(-1)
		cont = True
		if q < Q-1:
			q = q + 1
			d = int(inputs())
		else:
			flag = True
			q = q + 1
			break
	if flag == True:
		break
	if e[1] == 1:
		my_add(e[2])
		if mini > e[2]:
			mini = e[2]
	elif e[1] == -1:
		my_discard(e[2])
		cont = False
for t in [None]*(Q-q):
	print(-1)

Buggy submission (verdict: Runtime Error):
import sys
from operator import itemgetter
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
E = [None]*2*N
for n in range(N):
	a,b,c = map(int,inputs().split())
	E[2*n] = (a - c, 1, c)
	E[2*n + 1] = (b - c, -1, c)
E.sort(key=itemgetter(0))
tuple(E)
mini = 1000000001
kouho = {1000000001}
my_add = kouho.add
my_discard = kouho.remove
q = 0
d = int(inputs())
flag = False
cont = False
for e in E:
	while(e[0] > d):
		print(kouho)
		if cont == True:
			if mini < 1000000001:
				print(mini)
			else:
				print(-1)
		else:
			mini = min(kouho)
			if mini < 1000000001:
				print(mini)
			else:
				print(-1)
		cont = True
		if q < Q-1:
			q = q + 1
			d = int(inputs())
		else:
			flag = True
			q = q + 1
			break
	if flag == True:
		break
	if e[1] == 1:
		my_add(e[2])
		if mini > e[2]:
			mini = e[2]
	elif e[1] == -1:
		my_discard(e[2])
		cont = False
for t in [None]*(Q-q):
	print(-1)

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
V = [None]*N
E = []
for n in range(N):
	V[n] = list(map(int,inputs().split()))
	V[n][0] -= V[n][2]
	V[n][1] -= V[n][2]
	E.append([V[n][0],1,V[n][2]])
	E.append([V[n][1],-1,V[n][2]])
D = [int(inputs()) for p in [0]*Q]
E.sort(key = lambda x:x[0])
mini = 1000000001
kouho = [1000000001]
q = 0
d = D[0]
for e in E:
	flag = 0
	if e[0] <= d:
		if  e[1] == 1:
			kouho.append(e[2])
		elif e[1] == -1:
			kouho.remove(e[2])
	if e[0] > d:
		for k in kouho:
			mini = min(k,mini)
		if mini < 1000000001:
			print(mini)
			mini = 1000000001
		else:
			print(-1)
			mini = 1000000001
		flag = 1
		if q < Q-1:
			q = q + 1
			d = D[q]
		else:
			break
	if e[0] <= d and flag == 1:
		if  e[1] == 1:
			kouho.append(e[2])
		elif e[1] == -1:
			kouho.remove(e[2])
for t in [None]*(Q-q):
	print(-1)

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
V = [None]*N
E = []
for n in range(N):
	V[n] = list(map(int,inputs().split()))
	V[n][0] -= V[n][2]
	V[n][1] -= V[n][2]
	E.append([V[n][0],1,V[n][2]])
	E.append([V[n][1],-1,V[n][2]])
D = [int(inputs()) for p in [0]*Q]
E.sort(key = lambda x:x[0])
E.append([1000000001,0,1000000001])
mini = 1000000001
kouho = [1000000001]
q = 0
d = D[0]
for e in E:
	flag = 0
	if e[0] <= d:
		if  e[1] == 1:
			kouho.append(e[2])
		elif e[1] == -1:
			kouho.remove(e[2])
	if e[0] > d:
		for k in kouho:
			mini = min(k,mini)
		if mini < 1000000001:
			print(mini)
			mini = 1000000001
		else:
			print(-1)
			mini = 1000000001
		flag = 1
		if q < Q-1:
			q = q + 1
			d = D[q]
		else:
			break
	if e[0] <= d and flag == 1:
		if  e[1] == 1:
			kouho.append(e[2])
		elif e[1] == -1:
			kouho.remove(e[2])
for t in [None]*(Q-q-1):
	print(-1)