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:
#!/usr/bin/env python3
#ABC128 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,q = LI()
stx = [LI() for _ in range(n)]
d = [I() for _ in range(q)]
stx.sort(key = lambda x:(x[2],x[0]))
ans = [-1]*q
for s,t,x in stx:
    l = bl(d,s-x)
    r = bl(d,t-x)
    for i in range(l,r):
        if ans[i] < 0:
            ans[i] = x
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, Q = map(int, raw_input().split())
block = np.empty([N, 3], dtype=int)
D = np.empty(Q, dtype=int)
for i in xrange(N):
    block[i] = map(int, raw_input().split())

for i in xrange(Q):
    D[i] = int(raw_input())

block_position_sort = np.argsort(block[:,2])

position = -D
final_position = -1 * np.ones(Q, dtype=int)
not_stop = np.ones(Q, dtype=bool)
for i in xrange(N):
    St, Et, P = block[block_position_sort[i]]
    I = np.where(np.logical_and(position[not_stop]+St-1 < P, position[not_stop] + Et - 1 >= P ))[0]
    stop = np.where(not_stop)[0][I]
    not_stop[stop] = False
    final_position[stop] = P
    
for i in xrange(Q):
    print final_position[i]

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
INF = float('inf')

class MinSegTree:
    """
    0-indexed
    query : [L, R)
    """
    def __init__(self, size):
        self.size = 1 << (size.bit_length())  # 完全二分木にする
        self.data = [INF] * (2 * self.size - 1)

    def build(self, rawData):
        self.data[self.size - 1: self.size - 1 + len(rawData)] = rawData
        for i in range(self.size - 1)[:: -1]:
            self.data[i] = min(self.data[2 * i + 1], self.data[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.data[index] = value
        while index >= 0:
            index = (index - 1) // 2
            self.data[index] = min(self.data[2 * index + 1], self.data[2 * index + 2])

    def query(self, left, right):
        L = left + self.size
        R = right + self.size
        ret = INF
        while L < R:
            if R & 1:
                R -= 1
                ret = min(ret, self.data[R - 1])
            if L & 1:
                ret = min(ret, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return ret

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

events = []
for i in range(N):
    S, T, X = map(int, input().split())
    events.append((S - X, 0, i, X))
    events.append((T - X, 1, i, X))

for _ in range(Q):
    events.append((int(input()), 2, 0, 0))
events.sort()

tree = MinSegTree(N)

ans = []
for t, flag, index, x in events:
    if flag == 0:
        tree.update(index, x)
    elif flag == 1:
        tree.update(index, INF)
    else:
        print(tree.data[0] if tree.data[0] != INF else -1)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from heapq import heappop, heappush

N, Q = map(int, input().split())
INF = float('inf')

events = []
for i in range(N):
    S, T, X = map(int, input().split())
    events.append((S - X, 0, X))
    events.append((T - X, 1, X))

for _ in range(Q):
    events.append((int(input()), 2, 0))

events.sort()

cnt = defaultdict(int)
cnt[INF] += 1
que = [INF]

for t, flag, x in events:
    if flag == 0:
        if cnt[x] == 0:
            heappush(que, x)
        cnt[x] += 1
    elif flag == 1:
        cnt[x] -= 1
    else:
        while cnt[que[0]] == 0:
            heappop(que)
        print(que[0] if que[0] != INF else -1)

Submission status: Time Limit Exceeded

Solution code:
INF = float('inf')

class MinSegTree:
    """
    0-indexed
    query : [L, R)
    """
    def __init__(self, size):
        self.size = 1 << (size.bit_length())  # 完全二分木にする
        self.data = [INF] * (2 * self.size - 1)

    def build(self, rawData):
        self.data[self.size - 1: self.size - 1 + len(rawData)] = rawData
        for i in range(self.size - 1)[:: -1]:
            self.data[i] = min(self.data[2 * i + 1], self.data[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.data[index] = value
        while index >= 0:
            index = (index - 1) // 2
            self.data[index] = min(self.data[2 * index + 1], self.data[2 * index + 2])

    def query(self, left, right):
        L = left + self.size
        R = right + self.size
        ret = INF
        while L < R:
            if R & 1:
                R -= 1
                ret = min(ret, self.data[R - 1])
            if L & 1:
                ret = min(ret, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return ret

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

events = []
for i in range(N):
    S, T, X = map(int, input().split())
    events.append((S - X, 0, i, X))
    events.append((T - X, 1, i, X))

for _ in range(Q):
    events.append((int(input()), 2, 0, 0))
events.sort()

tree = MinSegTree(N)

ans = []
for t, flag, index, x in events:
    if flag == 0:
        tree.update(index, x)
    elif flag == 1:
        tree.update(index, INF)
    else:
        ans.append(tree.data[0])

for a in ans:
    print(-1 if a == INF else a)

Submission status: Time Limit Exceeded

Solution code:
INF = float('inf')

class MinSegTree:
    """
    0-indexed
    query : [L, R)
    """
    def __init__(self, size):
        self.size = 1 << (size.bit_length())  # 完全二分木にする
        self.data = [INF] * (2 * self.size - 1)

    def build(self, rawData):
        self.data[self.size - 1: self.size - 1 + len(rawData)] = rawData
        for i in range(self.size - 1)[:: -1]:
            self.data[i] = min(self.data[2 * i + 1], self.data[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.data[index] = value
        while index >= 0:
            index = (index - 1) // 2
            self.data[index] = min(self.data[2 * index + 1], self.data[2 * index + 2])

    def query(self, left, right):
        L = left + self.size
        R = right + self.size
        ret = INF
        while L < R:
            if R & 1:
                R -= 1
                ret = min(ret, self.data[R - 1])
            if L & 1:
                ret = min(ret, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return ret

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

events = []
for i in range(N):
    S, T, X = map(int, input().split())
    events.append((S - X, True, i, X))
    events.append((T - X, False, i, X))
events.sort()

tree = MinSegTree(N)

D = [int(input()) for _ in range(Q)]
M = len(events)
now = 0
ans = []
for d in D:
    while now < M and events[now][0] <= d:
        _, isStart, i, x = events[now]
        if isStart:
            tree.update(i, x)
        else:
            tree.update(i, INF)
        now += 1
    ans.append(tree.data[0])

for a in ans:
    print(-1 if a == INF else a)

Submission status: Time Limit Exceeded

Solution code:
INF = float('inf')

class MinSegTree:
    """
    0-indexed
    query : [L, R)
    """
    def __init__(self, size):
        self.size = 1 << (size.bit_length())  # 完全二分木にする
        self.data = [INF] * (2 * self.size - 1)

    def build(self, rawData):
        self.data[self.size - 1: self.size - 1 + len(rawData)] = rawData
        for i in range(self.size - 1)[:: -1]:
            self.data[i] = min(self.data[2 * i + 1], self.data[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.data[index] = value
        while index >= 0:
            index = (index - 1) // 2
            self.data[index] = min(self.data[2 * index + 1], self.data[2 * index + 2])

    def query(self, left, right):
        L = left + self.size
        R = right + self.size
        ret = INF
        while L < R:
            if R & 1:
                R -= 1
                ret = min(ret, self.data[R - 1])
            if L & 1:
                ret = min(ret, self.data[L - 1])
                L += 1
            L >>= 1
            R >>= 1
        return ret

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

events = []
for i in range(N):
    S, T, X = map(int, input().split())
    events.append((S - X, True, i, X))
    events.append((T - X, False, i, X))
events.sort()

tree = MinSegTree(N)

D = [int(input()) for _ in range(Q)]
M = len(events)
now = 0
ans = []
for d in D:
    while now < M and events[now][0] <= d:
        _, isStart, i, x = events[now]
        if isStart:
            tree.update(i, x)
        else:
            tree.update(i, INF)
        now += 1
    ans.append(tree.query(0, N))

for a in ans:
    print(-1 if a == INF else a)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())

STX = [tuple(map(int, input().split())) for _ in range(N)]
STX.sort(key=lambda A: A[2])

ans = []
for _ in range(Q):
    D = int(input())
    for s, t, x in STX:
        if s - x <= D < t - x:
            ans.append(x)
            break
    else:
        ans.append(-1)

print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree :
    def __init__(self, n) :
        self.n = n
        self.N0 = 1 << n.bit_length()
        self.data = [10**15] * (self.N0*2)
 
    def update(self, l, r, val) :
        l += self.N0
        r += self.N0
        while l < r:
            if l & 1 :
                self.data[l] = min(self.data[l], val)
                l += 1
            if r & 1:
                self.data[r-1] = min(self.data[r-1], val)
                r -= 1
            l //= 2
            r //= 2
 
    def query(self, i) :
        i += len(self.data) // 2
        ret = self.data[i]
        while i > 0 :
            i //= 2
            ret = min(ret, self.data[i])
        return ret if ret < 10**15 else -1
import bisect
N, Q = map(int, input().split())
roads = []
for _ in range(N):
    s, t, x = map(int, input().split())
    roads.append((x, s, t))
compress_list = []
query = []
for _ in range(Q):
    d = int(input())
    compress_list.append(d)
S = SegmentTree(Q)
for x, s, t in roads:
    l = bisect.bisect_left(compress_list, s-x)
    r = bisect.bisect_left(compress_list, t-x)
    S.update(l, r, x)
for i in range(Q):
    print(S.query(i))

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree :
    def __init__(self, n) :
        self.n = n
        self.N0 = 1 << n.bit_length()
        self.data = [10**15] * (self.N0*2)
 
    def update(self, l, r, val) :
        l += self.N0
        r += self.N0
        while l < r:
            if l & 1 :
                self.data[l] = min(self.data[l], val)
                l += 1
            if r & 1:
                self.data[r-1] = min(self.data[r-1], val)
                r -= 1
            l //= 2
            r //= 2
 
    def query(self, i) :
        i += len(self.data) // 2
        ret = self.data[i]
        while i > 0 :
            i //= 2
            ret = min(ret, self.data[i])
        return ret if ret < 10**15 else -1
import bisect
N, Q = map(int, input().split())
roads = []
for _ in range(N):
    s, t, x = map(int, input().split())
    roads.append((x, s, t))
compress_list = []
query = []
for _ in range(Q):
    d = int(input())
    compress_list.append(d)
    query.append(d)
compress_list.sort()
S = SegmentTree(Q)
for x, s, t in roads:
    l = bisect.bisect_left(compress_list, s-x)
    r = bisect.bisect_left(compress_list, t-x)
    S.update(l, r, x)
for d in query:
    i = bisect.bisect_left(compress_list, d)
    print(S.query(i))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left, insort_left
from collections import deque

input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    E, nums = deque(), []
    
    for i in range(N):
        s, t, x = map(int, input().split())
        
        E.extend([(s-x-0.5, 1, x), (t-x-0.5, -1, x)])
        
    E.extend([(int(input()), 0, 0) for i in range(Q)])
    E = sorted(E)
    
    ans = ['-1'] * Q
    idx = 0
    
    for e in E:
        d, m, x = e
        
        if m == 1: insort_left(nums, x)
        elif m == -1: del nums[bisect_left(nums, x)]
        else:
            if nums: ans[idx] = str(nums[0])
            idx += 1
            if idx == Q: break    
        
    print('\n'.join(ans))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left, insort_left
from collections import deque

input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    E, nums = deque(), []
    
    for i in range(N):
        s, t, x = map(int, input().split())
        
        E.extend([(s-x-0.5, 1, x), (t-x-0.5, -1, x)])
        
    E.extend([(int(input()), 0, 0) for i in range(Q)])
    E = sorted(E)
    
    ans = ['-1'] * Q
    idx = 0
    
    for e in E:
        d, m, x = e
        
        if m == 1: insort_left(nums, x)
        elif m == -1: del nums[bisect_left(nums, x)]
        elif nums: 
            ans[idx] = str(nums[0])
            idx += 1
        else:  idx += 1
        
    print('\n'.join(ans))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left, insort_left
from collections import deque

#input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    E, nums = deque(), []
    
    for i in range(N):
        s, t, x = map(int, input().split())
        
        E.extend([(s-x-0.5, 1, x), (t-x-0.5, -1, x)])
        
    E.extend([(int(input()), 0, 0) for i in range(Q)])
    E = sorted(E)
    
    ans = ['-1'] * Q
    idx = 0
    
    for e in E:
        d, m, x = e
        
        if m == 1: insort_left(nums, x)
        elif m == -1: del nums[bisect_left(nums, x)]
        elif nums: 
            ans[idx] = str(nums[0])
            idx += 1
        else:  idx += 1
        
    print('\n'.join(ans))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left, insort_left
from collections import deque

input = sys.stdin.readline

def main():
    N, Q = map(int, input().split())
    E, nums = deque(), []
    
    for i in range(N):
        s, t, x = map(int, input().split())
        
        E.extend([(s-x-0.5, 1, x), (t-x-0.5, -1, x)])
        
    E.extend([(int(input()), 0, 0) for i in range(Q)])
    E = sorted(E)
    
    for e in E:
        d, m, x = e
        
        if m == 1: insort_left(nums, x)
        elif m == -1: del nums[bisect_left(nums, x)]
        elif nums: print(nums[0])
        else: print(-1)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    n,q=map(int,input().split())
    xst=[]
    for i in range(n):
        s,t,x=map(int,input().split())
        xst.append([x,s,t])
    xst.sort()
    d=[None]*q
    for i in range(q):
        d[i]=[int(input()),i]
    ans=[-1]*q
    for u in xst:
        x,s,t=u
        left=s-x
        right=t-x-1
        a=bisect(d,[left])
        b=bisect(d,[right,q+1])
        for u in d[a:b]:
            ans[u[1]]=x
        d=list(itertools.chain(d[:a],d[b:]))
    for u in ans:
        print(u)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    n,q=map(int,input().split())
    xst=[]
    for i in range(n):
        s,t,x=map(int,input().split())
        xst.append([x,s,t])
    xst.sort()
    d=[None]*q
    for i in range(q):
        d[i]=[int(input()),i]
    ans=[-1]*q
    for u in xst:
        x,s,t=u
        left=s-x
        right=t-x-1
        a=bisect(d,[left])
        b=bisect(d,[right,q+1])
        for u in d[a:b]:
            ans[u[1]]=x
        d=d[:a]+d[b:]
    for u in ans:
        print(u)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect,bisect_left
n,q=map(int,input().split())
xst=[]
for i in range(n):
    s,t,x=map(int,input().split())
    xst.append([x,s,t])
xst.sort()
d=[]
for i in range(q):
    D=int(input())
    d.append([D,i])
ans=[-1]*q
for u in xst:
    x,s,t=u
    left=s-x
    right=t-x-1
    a=bisect(d,[left])
    b=bisect(d,[right,q+1])
    for u in d[a:b]:
        ans[u[1]]=x
    d=d[:a]+d[b:]
for u in ans:
    print(u)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, p = LI()
    print((a*3+p)//2)
    return

#B
def B():
    n = II()
    sp = []
    for i in range(n):
        sp.append(input().split())
    another = [(s[0], -1 * int(s[1]),num) for num,s in enumerate(sp)]
    another.sort()
    for s,p,q in another:
        print(q+1)

    return

#C
def C():
    n, m = LI()
    ks = LIR_(m)
    p = LI()
    patern = itertools.product(range(2), repeat=n)
    ans = 0
    for on in patern:
        ki = [0 for i in range(m)]
        for num, oni in enumerate(on):
            if oni:
                for i,ksi in enumerate(ks):
                    ksi = ksi[1::]
                    if num in ksi:
                        ki[i] ^= 1
        for num, pi in enumerate(p):
            if pi != ki[num]:
                break
        else:
            ans += 1
    print(ans)

            
    return

#D
def D():
    N, K = LI()#random.randint(1,50),random.randint(1,100)
    v = LI()#[random.randint(-10 ** 2, 10 ** 2) for i in range(N)]
    ans = 0
    for a in range(min(N, K) + 1):
        for b in range(min(N, K) + 1):
            if a + b > min(K,N):
                break
            if a == b == 0:
                continue
            deletes = K - a - b
            left = v[:a]
            right = v[:-b-1:-1]
            x = left + right
            x.sort()
            tmp = sum(x)
            for delete in range(min(deletes, len(x))):
                if x[delete] >= 0:
                    break
                tmp -= x[delete]
            ans = max(ans,tmp)
    
    print(ans)
                

    return


#E
def E():  
    N,q = LI()
    STX = LIR(N)
    D = IR(q)
    LV = (q - 1).bit_length()
    N0 = 1 << LV

    data = [inf] * (N0 << 1)
    INF = inf

    def update(l, r, x):
    
        #  区間[l, r)のdataの値を更新
        L = N0 + l; R = N0 + r
        while L < R:
            if R & 1:
                R -= 1
                data[R] = min(data[R-1], x)
            if L & 1:
                data[L] = min(data[L-1], x)
                L += 1
            L >>= 1; R >>= 1

    def query(x):
        L = N0 + x
        #  xの最小値を求める
        s = INF
        while L >= 0:
            s = min(s, data[L])
            L >>= 1
        return s

    for s, t, x in STX:
        XS = max(0, s - x)
        XT = max(0, t - x)
        XS_index = bisect_left(D, XS)
        XT_index = bisect_left(D, XT)
        if XS_index == XT_index:
            continue
        update(XS_index, XT_index, x)

    for i in range(q):
        a = query(i)
        print(a if a != inf else -1)
#F
def F():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
n,q=map(int,input().split())
a=[]
for i in range(n):
  s,t,x=map(int,input().split())
  s-=x
  t-=x
  a.append([s,t,x])
a=sorted(a,reverse=True)
heap=[]
ans=[]
for i in range(q):
  d=int(input())
  while a and a[-1][0]<=d:
    e=a.pop()
    heappush(heap,(e[2],e[1]))
  while heap and heap[0][1]<=d:
    heappop(heap)
  if heap:
    ans.append(heap[0][0])
  else:
    ans.append(-1)

print(*ans, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
n,q=map(int,input().split())
a=[]
for i in range(n):
  s,t,x=map(int,input().split())
  s-=x
  t-=x
  a.append([s,t,x])
a=sorted(a,reverse=True)
heap=[]
ans=[]
for i in range(q):
  d=int(input())
  while a and a[-1][0]<=d:
    e=a.pop()
    heappush(heap,(e[2],e[1]))
  while heap and heap[0][1]<=d:
    heappop(heap)
  if heap:
    ans.append(heap[0][0])
  else:
    ans.append(-1)

for i in ans:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left

N, Q = map(int, input().split())
E, nums = [], []

# 1...add, -1...delete
for i in range(N):
    s, t, x = map(int, input().split())
    
    E.extend([(s-x-0.5, 1, x), (t-x-0.5, -1, x)])
    
E.extend([(int(input()), 0, 0) for i in range(Q)])
E.sort()

for e in E:
    d, m, x = e
    
    if m == 1: insort_left(nums, x)
    elif m == -1: del nums[bisect_left(nums, x)]
    elif nums: print(nums[0])
    else: print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.buffer.readline().rstrip()
read = sys.stdin.buffer.read
def resolve():
    n,q=map(int,input().split())
    data = list(map(int,read().split()))
    S = data[:3*n:3]
    T = data[1:3*n:3]
    X = data[2:3*n:3]
    D = data[3*n:]

    E=[]
    E+=[(s-x,1,x) for s,x in zip(S,X)]
    E+=[(t-x,2,x) for t,x in zip(T,X)]
    E+=[(d,3,i) for i,d in enumerate(D)]
    E.sort()
    from collections import defaultdict
    cnt=defaultdict(int)
    cnt[INF]=1

    from heapq import heappop,heappush
    Q=[INF]
    ans=[None]*q
    for a,b,c in E:
        if(b==1):
            heappush(Q,c)
            cnt[c]+=1
        elif(b==2): # 本来はここでqからcを削除したいが、できないためb==3で処理する
            cnt[c]-=1
        else:
            while(cnt[Q[0]]==0): # 本来削除されるべき通行止めをここで消す
                heappop(Q)
            x=Q[0]
            ans[c]=x if(x!=INF) else -1
    print(*ans,sep="\n")
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.buffer.readline().rstrip()
def resolve():
    n,q=map(int,input().split())
    S,T,X=[0]*n,[0]*n,[0]*n
    D=[0]*q
    for i in range(n):
        S[i],T[i],X[i]=map(int,input().split())
    for j in range(q):
        D[j]=int(input())

    E=[]
    E+=[(s-x,1,x) for s,x in zip(S,X)]
    E+=[(t-x,2,x) for t,x in zip(T,X)]
    E+=[(d,3,i) for i,d in enumerate(D)]
    E.sort()
    from collections import defaultdict
    cnt=defaultdict(int)
    cnt[INF]=1

    from heapq import heappop,heappush
    Q=[INF]
    ans=[-1]*q
    for a,b,c in E:
        if(b==1):
            heappush(Q,c)
            cnt[c]+=1
        elif(b==2): # 本来はここでqからcを削除したいが、できないためb==3で処理する
            cnt[c]-=1
        else:
            while(cnt[Q[0]]==0): # 本来削除されるべき通行止めをここで消す
                heappop(Q)
            x=Q[0]
            if(x!=INF): ans[c]=x
    print(*ans,sep="\n")
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.buffer.readline().rstrip()
def resolve():
    n,q=map(int,input().split())
    S,T,X=[0]*n,[0]*n,[0]*n
    D=[0]*q
    for i in range(n):
        S[i],T[i],X[i]=map(int,input().split())
    for j in range(q):
        D[j]=int(input())

    E=[]
    E+=[(s-x,1,x) for s,x in zip(S,X)]
    E+=[(t-x,2,x) for t,x in zip(T,X)]
    E+=[(d,3,i) for i,d in enumerate(D)]
    E.sort()
    cnt={x:0 for x in X}
    cnt[INF]=1

    from heapq import heappop,heappush
    Q=[INF]
    ans=[-1]*q
    for a,b,c in E:
        if(b==1):
            heappush(Q,c)
            cnt[c]+=1
        elif(b==2): # 本来はここでqからcを削除したいが、できないためb==3で処理する
            cnt[c]-=1
        else:
            while(cnt[Q[0]]==0): # 本来削除されるべき通行止めをここで消す
                heappop(Q)
            x=Q[0]
            if(x!=INF): ans[c]=x
    print(*ans,sep="\n")
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,q=map(int,input().split())
    S,T,X=[0]*n,[0]*n,[0]*n
    D=[0]*q
    for i in range(n):
        S[i],T[i],X[i]=map(int,input().split())
    for j in range(q):
        D[j]=int(input())

    E=[]
    E+=[(s-x,1,x) for s,x in zip(S,X)]
    E+=[(t-x,2,x) for t,x in zip(T,X)]
    E+=[(d,3,i) for i,d in enumerate(D)]
    E.sort()
    cnt={x:0 for x in X}
    cnt[INF]=1

    from heapq import heappop,heappush
    Q=[INF]
    ans=[-1]*q
    for a,b,c in E:
        if(b==1):
            heappush(Q,c)
            cnt[c]+=1
        elif(b==2): # 本来はここでqからcを削除したいが、できないためb==3で処理する
            cnt[c]-=1
        else:
            while(cnt[Q[0]]==0): # 本来削除されるべき通行止めをここで消す
                heappop(Q)
            x=Q[0]
            if(x!=INF): ans[c]=x
    print(*ans,sep="\n")
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import heapq
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
L1=[]
for i in range(n):
  L1.append([l[i][0]-l[i][2],1,l[i][2]])
  L1.append([l[i][1]-l[i][2],-1,l[i][2]])
for i in range(q):
  L1.append([int(input()),2,0])
L1.sort()
L2=[]
heapq.heapify(L2)
L3=[]
L3=set(L3)
ans=[]
for i in range(len(L1)):
  if L1[i][1]==1:
    heapq.heappush(L2,L1[i][2])
    L3.add(L1[i][2])
  elif L1[i][1]==-1:
    L3.remove(L1[i][2])
  else:
    while L2 and L2[0] not in L3:
      heapq.heappop(L2)
    if L2:
      print(L2[0])
    else:
      print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import heapq
n,q=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
d=[int(input()) for i in range(q)]
L1=[]
for i in range(n):
  L1.append([l[i][0]-l[i][2],1,l[i][2]])
  L1.append([l[i][1]-l[i][2],-1,l[i][2]])
L1.sort()
L2=[]
heapq.heapify(L2)
L3=[]
L3=set(L3)
ctd=0
for i in range(len(L1)):
  if ctd<q:
    while L1[i][0]>d[ctd]:
      if len(L2)!=0:
        while L2[0] not in L3:
          heapq.heappop(L2)
          if len(L2)==0:
            heapq.heappush(L2,-1)
            break
        x=heapq.heappop(L2)
        print(x)
        if x!=-1:
          heapq.heappush(L2,x)
      else:
        print(-1)
      ctd+=1
      if ctd==q:
        break
  else:
    break
  if L1[i][1]==1:
    heapq.heappush(L2,L1[i][2])
    L3.add(L1[i][2])
  else:
    L3.remove(L1[i][2])
while ctd<q:
  print(-1)
  ctd+=1

Submission status: Time Limit Exceeded

Solution code:
# instead of AVLTree
class BITbisect():
    def __init__(self, max):
        self.max = max
        self.data = [0]*(self.max+1)
    
    # 0からiまでの区間和
    # 立っているビットを下から処理
    def query_sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    # i番目の要素にxを足す
    # 覆ってる区間すべてに足す
    def add(self, i, x):
        while i <= self.max:
            self.data[i] += x
            i += i & -i

    def insert(self, x):
        self.add(x, 1)

    def delete(self, x):
        self.add(x, -1)

    def count(self, x):
        return self.query_sum(x) - self.query_sum(x-1)
    
    def length(self):
        return self.query_sum(self.max)
    
    # 下からc番目(0-indexed)の数
    # O(log(N))
    def search(self, c):
        c += 1
        s = 0
        ind = 0
        l = self.max.bit_length()
        for i in reversed(range(l)):
            if ind + (1<<i) <= self.max:
                if s + self.data[ind+(1<<i)] < c:
                    s += self.data[ind+(1<<i)]
                    ind += (1<<i)
        if ind == self.max:
            return False
        return ind + 1
    
    def bisect_right(self, x):
        return self.query_sum(x)

    def bisect_left(self, x):
        if x == 1:
            return 0
        return self.query_sum(x-1)

    # listみたいに表示
    def display(self):
        print('inside BIT:', end=' ')
        for x in range(1, self.max+1):
            if self.count(x):
                c = self.count(x)
                for _ in range(c):
                    print(x, end=' ')
        print()

from bisect import bisect_left, bisect_right
from operator import itemgetter
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
STX = [list(map(int, input().split())) for _ in range(N)]
D = [int(input()) for _ in range(Q)]

bit = BITbisect(Q+1)

for i, d in enumerate(D):
    bit.insert(i+1)

STX.sort(key=itemgetter(2))

ans = [-1]*Q
for s, t, x in STX:
    #print(s, t, x)
    #bit.display()
    il = bisect_left(D, s-x)
    ir = bisect_right(D, t-x-1)
    #print(il, ir)
    ind_l = bit.bisect_right(il)
    ind_r = bit.bisect_right(ir)
    #print(ind_l, ind_r)
    for ind in range(ind_l, ind_r):
        il = bit.search(ind_l)
        bit.delete(il)
        ans[il-1] = x


for a in ans:
    print(a)