Problem:


Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

1 \leq N, Q \leq 2 \times 10^5

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.



Sample input:
4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8


Sample output:
2
2
10
-1
13
-1

Submission status: Time Limit Exceeded

Solution code:
# ABC128 E
from bisect import bisect_right as br
from bisect import bisect_left as bl

N,Q=map(int,input().split())
closed=[]
for _ in [0]*N:
    closed.append(tuple(map(int,input().split())))
closed.sort(key=lambda x:x[2])
    
D=[-int(input()) for _ in [0]*Q][::-1]

walk=[-1]*Q
for s,t,x in closed:
    ind=bl(D,x-(t-0.5))
    loop=br(D,x-(s-0.5))-ind
    for i in range(loop):
        if walk[ind+i]!=-1:
            continue
        else:
            walk[ind+i]=x
print(*walk[::-1],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
from bisect import bisect_left
n,q=map(int,input().split())
stop=[]
for i in range(n):
	s,t,x=map(int,input().split())
	stop.append((s,t,x))
stop.sort(key=lambda x:x[2])
d=[int(input())for i in range(q)]

ans=[-1]*q
for s,t,x in stop:
    l=bisect_left(d,s-x)
    r=bisect_left(d,t-x)
    for i in range(l, r):
        if ans[i] == -1:
            ans[i] = x
            
for x in ans:
	print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
from bisect import bisect_left

N, Q = [int(x) for x in input().split()]
data = []
for i in range(N):
    S, T, X = [int(x) for x in input().split()]
    data.append([X, S, T])
data.sort()
D = [int(input()) for _ in range(Q)]

ans = [-1]*Q
for X, S, T in data:
    l = bisect_left(D, S - X)
    r = bisect_left(D, T - X)
    for i in range(l, r):
        if ans[i] == -1:
            ans[i] = X

for i in range(Q):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import bisect

N, Q = [int(x) for x in input().split()]
data = []
for i in range(N):
    S, T, X = [int(x) for x in input().split()]
    data.append([X, S, T])
data.sort()
D = [int(input()) for _ in range(Q)]

ans = [-1]*Q
for X, S, T in data:
    l = bisect.bisect_left(D, S - X - 0.5)
    r = bisect.bisect_right(D, T - X - 0.5)
    for i in range(l, r):
        if ans[i] == -1:
            ans[i] = X

for i in range(Q):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect

N, Q = [int(x) for x in input().split()]
data = []
for i in range(N):
    S, T, X = [int(x) for x in input().split()]
    data.append([X, S, T])
data.sort()
D = [int(input()) for _ in range(Q)]

ans = [-1]*Q
for X, S, T in data:
    l = bisect.bisect_left(D, S - X - 0.5)
    r = bisect.bisect_right(D, T - X - 0.5)
    for i in range(l, r):
        if ans[i] == -1:
            ans[i] = X

for i in range(Q):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect

N, Q = [int(x) for x in input().split()]
data = []
for i in range(N):
    S, T, X = [int(x) for x in input().split()]
    data.append([X, S, T])
data.sort()
D = [int(input()) for _ in range(Q)]

ans = [-1]*Q
for X, S, T in data:
    l = bisect.bisect_left(D, S - X - 0.5)
    r = bisect.bisect_right(D, T - X - 0.5)
    for i in range(l, r):
        if ans[i] == -1:
            ans[i] = X

for i in range(Q):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import bisect

INF = 10**9 + 1

N, Q = [int(x) for x in input().split()]

data = []
for i in range(N):
    S, T, X = [int(x) for x in input().split()]
    data.append([X, S, T])
data = sorted(data, reverse=True)

D = []
for i in range(Q):
    D.append(int(input()))

dist = [INF]*Q
for i in range(N):
    [X, S, T] = data[i]
    l = bisect.bisect_left(D, S - X - 0.5)
    r = bisect.bisect_right(D, T - X - 0.5)
    dist = dist[:l] + [X]*(r - l) + dist[r:]

for i in range(Q):
    if dist[i] == INF:
        print(-1)
    else:
        print(dist[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from operator import itemgetter
import sys
input = sys.stdin.buffer.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = itemgetter(2))
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for a in A:
		S, T, X = a
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if not j in donei:
				ans[j] = X
				donei.add(j)
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: x[2])
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	done = [False] * Q
	for a in A:
		S, T, X = a
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if not done[j]:
				ans[j] = X
				done[j] = True
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: (x[2], x[1] - x[0]))
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	done = [False] * Q
	for a in A:
		S, T, X = a
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if not done[j]:
				ans[j] = X
				done[j] = True
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: x[2])
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for a in A:
		S, T, X = a
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if not j in donei:
				ans[j] = X
				donei.add(j)
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = np.array([list(map(int, input().split())) for i in range(N)], dtype = np.int32)
	A = A[np.argsort(A[:, 2])]
	D = np.array([int(input()) for i in range(Q)], dtype = np.int32)
	ans = np.full(Q, -1)
	
	donecnt = 0
	donei = set()
	for i in range(N):
		S, T, X = A[i]
		for j in range(np.searchsorted(D, S - X), np.searchsorted(D, T - X)):
			if not j in donei:
				ans[j] = X
				donei.add(j)
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
				break
	for i in range(Q):
		print(ans[i])


if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: x[2])
	A = tuple(A)
	D = tuple([int(input()) for i in range(Q)])
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for i in range(N):
		for j in range(bisect_left(D, A[i][0] - A[i][2]), bisect_left(D, A[i][1] - A[i][2])):
			if not j in donei:
				ans[j] = A[i][2]
				donei.add(j)
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for i in range(Q):
		print(ans[i])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: x[2])
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for i in range(N):
		S, T, X = A[i]
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if not j in donei:
				ans[j] = X
				donei.add(j)
				donecnt += 1
				if donecnt == Q:
					break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = [tuple(map(int, input().split())) for i in range(N)]
	A.sort(key = lambda x: x[2])
	D = [int(input()) for i in range(Q)]
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for i in range(N):
		S, T, X = A[i]
		for j in range(bisect_left(D, S - X), bisect_left(D, T - X)):
			if j in donei:
				continue
			ans[j] = X
			donei.add(j)
			donecnt += 1
			if donecnt == Q:
				break
		if donecnt == Q:
			break
	for k in range(Q):
		print(ans[k])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = []
	for i in range(N):
		S, T, X = map(int, input().split())
		A.append((X, S, T))
	A.sort()
	D = []
	for i in range(Q):
		d = int(input())
		D.append(d)
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for i in range(N):
		X, S, T = A[i]
		l = bisect_left(D, S - X)
		r = bisect_left(D, T - X)
		for j in range(l, r):
			if j in donei:
				continue
			ans[j] = X
			donei.add(j)
			donecnt += 1
			if donecnt == Q:
				break
		if donecnt == Q:
			break
	
	for i in range(Q):
		print(ans[i])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
import sys
input = sys.stdin.readline

def main():
	N, Q = map(int, input().split())
	A = []
	for i in range(N):
		S, T, X = map(int, input().split())
		A.append((X, S, T))
	A.sort()
	D = []
	for i in range(Q):
		d = int(input())
		D.append([d, i])
	D.sort()
	ans = [-1] * Q
	
	donecnt = 0
	donei = set()
	for i in range(N):
		X, S, T = A[i]
		l = bisect(D, [S - X, -1])
		r = bisect(D, [T - X, -1])
		for j in range(l, r):
			if j in donei:
				continue
			ans[D[j][1]] = X
			donei.add(j)
			donecnt += 1
			if donecnt == Q:
				break
		if donecnt == Q:
			break
	
	for i in range(Q):
		print(ans[i])
		
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
A = []
for i in range(N):
	S, T, X = map(int, input().split())
	A.append((X, S, T))
A.sort()
D = []
for i in range(Q):
	d = int(input())
	D.append([d, i])
D.sort()
ans = [-1] * Q

donecnt = 0
donei = set()
for i in range(N):
	X, S, T = A[i]
	l = bisect(D, [S - X, -1])
	r = bisect(D, [T - X, -1])
	for j in range(l, r):
		if j in donei:
			continue
		ans[D[j][1]] = X
		donei.add(j)
		donecnt += 1
		if donecnt == Q:
			break
	if donecnt == Q:
		break

for i in range(Q):
	print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

N, Q = map(int, input().split())
A = []
for i in range(N):
	S, T, X = map(int, input().split())
	A.append((X, S, T))
A.sort()
D = []
for i in range(Q):
	d = int(input())
	D.append([d, i])
D.sort()
ans = [-1] * Q

donecnt = 0
donei = set()
for i in range(N):
	X, S, T = A[i]
	l = bisect(D, [S - X, -1])
	r = bisect(D, [T - X, -1])
	for j in range(l, r):
		if j in donei:
			continue
		ans[D[j][1]] = X
		donei.add(j)
		donecnt += 1
		if donecnt == Q:
			break
	if donecnt == Q:
		break

for i in range(Q):
	print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

N, Q = map(int, input().split())
A = []
for i in range(N):
	S, T, X = map(int, input().split())
	A.append((X, S, T))
A.sort()
D = []
for i in range(Q):
	d = int(input())
	D.append([d, i])
D.sort()
ans = [-1] * Q

donei = set()
for i in range(N):
	X, S, T = A[i]
	l = bisect(D, [S - X, -1])
	r = bisect(D, [T - X, -1])
	for j in range(l, r):
		if j in donei:
			continue
		ans[D[j][1]] = X
		donei.add(j)

for i in range(Q):
	print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
import collections
import bisect
import sys
sys.setrecursionlimit(10**9)

def zarts(X,x): # 座標圧縮
    return bisect.bisect_left(X,x)

    
INF=10**20
def solve(N: int, Q: int, S: "List[int]", T: "List[int]", X: "List[int]", D: "List[int]"):

    L = collections.defaultdict(lambda: [])
    R = collections.defaultdict(lambda: [])

    # heapq.heapify()
    for i in range(N):
        l = S[i] - X[i]
        r = T[i] - X[i]

        pos_l = zarts(D,l)
        pos_r = zarts(D,r)

        if pos_l == pos_r: continue
        bisect.insort_left(L[pos_l],X[i])
        bisect.insort_left(R[pos_r],X[i])
    

    worked = set()
    for q in range(Q):
        # print(worked)
        d = q
        ls = L[d]
        rs = R[d]

        while rs:
            worked.remove(rs.pop())
        
        while ls:
            worked.add(ls.pop())

        if not worked:
            print(-1)
            continue

        x = min(worked)
        print(x)



    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = [int()] * (N)  # type: "List[int]"
    T = [int()] * (N)  # type: "List[int]"
    X = [int()] * (N)  # type: "List[int]"
    # X = [(int(),int())] * (N)  # type: "List[int]"
    for i in range(N):
        S[i] = int(next(tokens))
        T[i] = int(next(tokens))
        X[i] = int(next(tokens))
        # X[i] = (-1 * int(next(tokens)),i)
    D = [int(next(tokens)) for _ in range(Q)]  # type: "List[int]"
    solve(N, Q, S, T, X, D)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
import collections
import bisect
import sys
sys.setrecursionlimit(10**9)

def zarts(X,x): # 座標圧縮
    return bisect.bisect_left(X,x)

    
INF=10**20
def solve(N: int, Q: int, S: "List[int]", T: "List[int]", X: "List[int]", D: "List[int]"):

    pos = D.copy()
    L = collections.defaultdict(lambda: [])
    R = collections.defaultdict(lambda: [])

    # heapq.heapify()
    for i in range(N):
        l = S[i] - X[i]
        r = T[i] - X[i]

        pos_l = zarts(D,l)
        pos_r = zarts(D,r)

        if pos_l == pos_r: continue
        bisect.insort_left(L[pos_l],X[i])
        bisect.insort_left(R[pos_r],X[i])
    

    worked = set()
    for q in range(Q):
        # print(worked)
        d = q
        ls = L[d]
        rs = R[d]

        while rs:
            worked.remove(rs.pop())
        
        while ls:
            worked.add(ls.pop())

        if not worked:
            print(-1)
            continue

        x = min(worked)
        print(x)



    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = [int()] * (N)  # type: "List[int]"
    T = [int()] * (N)  # type: "List[int]"
    X = [int()] * (N)  # type: "List[int]"
    # X = [(int(),int())] * (N)  # type: "List[int]"
    for i in range(N):
        S[i] = int(next(tokens))
        T[i] = int(next(tokens))
        X[i] = int(next(tokens))
        # X[i] = (-1 * int(next(tokens)),i)
    D = [int(next(tokens)) for _ in range(Q)]  # type: "List[int]"
    solve(N, Q, S, T, X, D)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import bisect
import sys
sys.setrecursionlimit(10**9)



# Ref: https://qiita.com/takayg1/items/b7b3f7d458915bcc7a4e
class LazySegTree:
    def __init__(self, init_val, segfunc, ide_ele = 2**31 - 1):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.data = [ide_ele] * 2 * self.num
        self.lazy = [None] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.data[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])



    def propagates(self, *ids):
        for i in reversed(ids):
            v = self.lazy[i]
            if v is None:
                continue
            self.lazy[2 * i] = v
            self.lazy[2 * i + 1] = v
            self.data[2 * i] = v
            self.data[2 * i + 1] = v
            self.lazy[i] = None

    def update(self, l, r, x): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                self.lazy[l] = x
                self.data[l] = x
                l += 1
            if r & 1:
                self.lazy[r - 1] = x
                self.data[r - 1] = x
            r >>= 1
            l >>= 1
        for i in ids:
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])


    def query(self, l, r): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)

        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.data[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.data[r - 1])
            l >>= 1
            r >>= 1
        return res

    def gindex(self, l, r):
        l += self.num
        r += self.num
        lm = l >> (l & -l).bit_length()
        rm = r >> (r & -r).bit_length()

        while r > l:
            if l <= lm:
                yield l
            if r <= rm:
                yield r
            r >>= 1
            l >>= 1
        while l:
            yield l
            l >>= 1

def zarts(D,x): # 座標圧縮
    return bisect.bisect_left(D,x)


def enumerate(X):
    return np.array([[i,X[i]] for i in range(len(X))])

def sort(X,key=0,reverse=False):
    Y = X[np.argsort(X[:,key])]
    if reverse:
        return Y[::-1]
    else:
        return Y
    

INF=10**20
def solve(N: int, Q: int, S: "List[int]", T: "List[int]", X: "List[int]", D: "List[int]"):
    L = S - X
    R = T - X

    def segfunc(x,y):
        return min(x,y)

    v = [INF] * (Q+1)

    seg = LazySegTree(v,segfunc,ide_ele=INF)

    X = enumerate(X)
    X = sort(np.array(X),key=1,reverse=True)

    for i,x in X:
        l = zarts(D,L[i])
        r = zarts(D,R[i])
        
        if l == r: continue
        seg.update(l,r,x)
    

    for q in range(Q):
        d = q
        # print("d",d)
        x = seg.query(d,d+1)
        if x == INF:
            x = -1

        print(x)
    
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = [int()] * (N)  # type: "List[int]"
    T = [int()] * (N)  # type: "List[int]"
    X = [int()] * (N)  # type: "List[int]"
    # X = [(int(),int())] * (N)  # type: "List[int]"
    for i in range(N):
        S[i] = int(next(tokens))
        T[i] = int(next(tokens))
        X[i] = int(next(tokens))
        # X[i] = (-1 * int(next(tokens)),i)
    D = [int(next(tokens)) for _ in range(Q)]  # type: "List[int]"

    S = np.array(S)
    T = np.array(T)
    X = np.array(X)
    D = np.array(D)


    solve(N, Q, S, T, X, D)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
#! /usr/bin/env python3
import bisect
import sys
sys.setrecursionlimit(10**9)



# Ref: https://qiita.com/takayg1/items/b7b3f7d458915bcc7a4e
class LazySegTree:
    def __init__(self, init_val, segfunc, ide_ele = 2**31 - 1):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.data = [ide_ele] * 2 * self.num
        self.lazy = [None] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.data[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])



    def propagates(self, *ids):
        for i in reversed(ids):
            v = self.lazy[i]
            if v is None:
                continue
            self.lazy[2 * i] = v
            self.lazy[2 * i + 1] = v
            self.data[2 * i] = v
            self.data[2 * i + 1] = v
            self.lazy[i] = None

    def update(self, l, r, x): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                self.lazy[l] = x
                self.data[l] = x
                l += 1
            if r & 1:
                self.lazy[r - 1] = x
                self.data[r - 1] = x
            r >>= 1
            l >>= 1
        for i in ids:
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])


    def query(self, l, r): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)

        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.data[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.data[r - 1])
            l >>= 1
            r >>= 1
        return res

    def gindex(self, l, r):
        l += self.num
        r += self.num
        lm = l >> (l & -l).bit_length()
        rm = r >> (r & -r).bit_length()

        while r > l:
            if l <= lm:
                yield l
            if r <= rm:
                yield r
            r >>= 1
            l >>= 1
        while l:
            yield l
            l >>= 1

def zarts(X): # 座標圧縮
    import collections
    Y = list(set(X))
    Y.sort()

    Z = collections.defaultdict(lambda: INF)
    for i,y in enumerate(Y):
        z = bisect.bisect_left(Y,y)
        Z[y] = z
    
    return Z
        
    

INF=10**20
def solve(N: int, Q: int, S: "List[int]", T: "List[int]", X: "List[int]", D: "List[int]"):

    def segfunc(x,y):
        return min(x,y)

    for i in range(N): 
        l = S[i]-X[i]
        r = T[i]-X[i]
        
        D.append(l)
        D.append(r)

    zarts_dict = zarts(D)
    v = [INF] * (len(D) + 1)

    seg = LazySegTree(v,segfunc,ide_ele=INF)

    _X = list(enumerate(X))
    _X.sort(key=lambda x:x[1],reverse=True)
    for i,x in _X:
        l = zarts_dict[S[i]-X[i]]
        r = zarts_dict[T[i]-X[i]]
        
        seg.update(l,r,X[i])
    

    for q in range(Q):
        d = zarts_dict[D[q]]
        # print("d",d)
        x = seg.query(d,d+1)
        if x == INF:
            x = -1

        print(x)
    
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = [int()] * (N)  # type: "List[int]"
    T = [int()] * (N)  # type: "List[int]"
    X = [int()] * (N)  # type: "List[int]"
    # X = [(int(),int())] * (N)  # type: "List[int]"
    for i in range(N):
        S[i] = int(next(tokens))
        T[i] = int(next(tokens))
        X[i] = int(next(tokens))
        # X[i] = (-1 * int(next(tokens)),i)
    D = [int(next(tokens)) for _ in range(Q)]  # type: "List[int]"
    solve(N, Q, S, T, X, D)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
import heapq
import bisect
import sys
sys.setrecursionlimit(10**9)



# Ref: https://qiita.com/takayg1/items/b7b3f7d458915bcc7a4e
class LazySegTree:
    def __init__(self, init_val, segfunc, ide_ele = 2**31 - 1):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.data = [ide_ele] * 2 * self.num
        self.lazy = [None] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.data[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])

    def gindex(self, l, r):
        l += self.num
        r += self.num
        lm = l >> (l & -l).bit_length()
        rm = r >> (r & -r).bit_length()

        while r > l:
            if l <= lm:
                yield l
            if r <= rm:
                yield r
            r >>= 1
            l >>= 1
        while l:
            yield l
            l >>= 1

    def propagates(self, *ids):
        for i in reversed(ids):
            v = self.lazy[i]
            if v is None:
                continue
            self.lazy[2 * i] = v
            self.lazy[2 * i + 1] = v
            self.data[2 * i] = v
            self.data[2 * i + 1] = v
            self.lazy[i] = None

    def update(self, l, r, x): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                self.lazy[l] = x
                self.data[l] = x
                l += 1
            if r & 1:
                self.lazy[r - 1] = x
                self.data[r - 1] = x
            r >>= 1
            l >>= 1
        for i in ids:
            self.data[i] = self.segfunc(self.data[2 * i], self.data[2 * i + 1])


    def query(self, l, r): # [l, r)
        *ids, = self.gindex(l, r)
        self.propagates(*ids)

        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.data[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.data[r - 1])
            l >>= 1
            r >>= 1
        return res

def zarts(X): # 座標圧縮
    import collections
    Y = list(set(X))
    Y.sort()

    Z = collections.defaultdict(lambda: INF)
    for i,y in enumerate(Y):
        z = bisect.bisect_left(Y,y)
        Z[y] = z
    
    return Z
        
    

INF=10**20
def solve(N: int, Q: int, S: "List[int]", T: "List[int]", X, D: "List[int]"):
    mx = 1000
    v = [INF] * mx
    def segfunc(x,y):
        return min(x,y)

    P = D.copy()
    for i in range(N): 
        x = -1 * X[i][0]
        l = S[i]-x
        r = T[i]-x

        P.append(l)
        P.append(r)

    zarts_dict = zarts(P)
    v = [INF] * (len(P) + 1)

    seg = LazySegTree(v,segfunc,ide_ele=INF)

    heapq.heapify(X)
    while X:
        x,i = heapq.heappop(X)
        x *= -1

        l = zarts_dict[S[i]-x]
        r = zarts_dict[T[i]-x]
        
        seg.update(l,r,x)
    

    for q in range(Q):
        d = zarts_dict[D[q]]
        # print("d",d)
        x = seg.query(d,d+1)
        if x == INF:
            x = -1

        print(x)
    
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = [int()] * (N)  # type: "List[int]"
    T = [int()] * (N)  # type: "List[int]"
    # X = [int()] * (N)  # type: "List[int]"
    X = [(int(),int())] * (N)  # type: "List[int]"
    for i in range(N):
        S[i] = int(next(tokens))
        T[i] = int(next(tokens))
        # X[i] = int(next(tokens))
        X[i] = (-1 * int(next(tokens)),i)
    D = [int(next(tokens)) for _ in range(Q)]  # type: "List[int]"
    solve(N, Q, S, T, X, D)



if __name__ == "__main__":
    main()