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:
#D in [S_i-X_i,T_i-X_i) → Xiで止まる
import sys
input = sys.stdin.readline

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  q.append((s-x,1,x)) # xで死ぬ
  q.append((t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  q.append((d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所

q.sort()

for a,c,b in q:
  if not c:
    se.remove(b)
  elif c&1:
    se.add(b)
  else:
    if se:
      answer[b] = min(se)

print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_right

input = sys.stdin.readline

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

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

n = N * 2
T, ans = [None] * n, [-1] * n
tmp = set()
for i in range(n):
    t, p, x = e[i]
    if p:
        tmp.add(x)
    else:
        tmp.remove(x)
    T[i] = t
    if len(tmp) > 0:
        ans[i] = min(tmp)

for _ in range(Q):
    D = int(input())
    i = bisect_right(T, D)
    if 0 <= i - 1 < n:
        print(ans[i - 1])
    else:
        print(-1)

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所
min_se = 10**18
flag = False # min_seが正しい値である

while q:
  a,c,b = heappop(q)
  if not c:
    se.remove(b)
    if b == min_se:
      flag = False
  elif c&1:
    se.add(b)
    if b < min_se:
      min_se = b
      flag = True
  else:
    if se:
      if not flag:
        min_se = min(se)
        flag = True
      answer[b] = min_se

print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = []
remove_task = []

while q:
  a,c,b = heappop(q)
  if not c:
    if b == se[0]:
      heappop(se)
      while remove_task and (se[0] == remove_task[0]):
        heappop(se)
        heappop(remove_task)
    else:
      heappush(remove_task,b)
  elif c&1:
    heappush(se,b)
  else:
    if se:
      answer[b] = se[0]
  #print(a,c,b,se,remove_task)
print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所

while q:
  a,c,b = heappop(q)
  if not c:
    se.remove(b)
  elif c&1:
    se.add(b)
  else:
    if se:
      answer[b] = min(se)

print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = []
remove_task = []

while q:
  a,c,b = heappop(q)
  if not c:
    heappush(remove_task,b)
  elif c&1:
    heappush(se,b)
  else:
    while remove_task and (se[0] == remove_task[0]):
      heappop(se)
      heappop(remove_task)
    if se:
      answer[b] = se[0]
  #print(a,c,b,se,remove_task)
print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from bisect import bisect_left

def main():
    N, Q = LI()
    XST = []
    for _ in range(N):
        XST.append(LI())
    D = []
    for _ in range(Q):
        D.append(II())
    ans = [INF] * len(D)
    for s, t, x in XST:
        s0 = bisect_left(D, s - x)
        t0 = bisect_left(D, t - x)
        for i in range(s0, t0):
            ans[i] = min(ans[i], x)
    for y in ans:
        print(-1 if y == INF else y)
    return

main()

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = []
remove_task = []

while q:
  a,c,b = heappop(q)
  if not c:
    if b == se[0]:
      heappop(se)
      while remove_task:
        x = se[0]
        y = remove_task[0]
        if x == y:
          heappop(se)
          heappop(remove_task)
          continue
        break
    else:
      heappush(remove_task,b)
  elif c&1:
    heappush(se,b)
  else:
    if se:
      answer[b] = se[0]
  #print(a,c,b,se,remove_task)
print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from bisect import bisect_left

def main():
    N, Q = LI()
    XST = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        XST.append([x, s, t])
    D = []
    for _ in range(Q):
        D.append(II())
    ans = [INF] * len(D)
    for x, s, t in XST:
        s0 = bisect_left(D, s)
        t0 = bisect_left(D, t)
        for i in range(s0, t0):
            ans[i] = min(ans[i], x)
    for y in ans:
        print(-1 if y == INF else y)
    return

main()

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる

for i in range(Q):
  d = int(input())
  heappush(q,(d,2,i))
answer = [-1]*Q

se = []
remove_task = []

while q:
  a,c,b = heappop(q)
  if not c:
    heappush(remove_task,b)
  elif c&1:
    heappush(se,b)
  else:
    while remove_task:
      x = se[0]
      y = remove_task[0]
      if x == y:
        heappop(se)
        heappop(remove_task)
        continue
      break
    if se:
      answer[b] = se[0]
  #print(a,c,b,se,remove_task)
print('\n'.join(map(str,answer)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from bisect import bisect_left

def main():
    N, Q = LI()
    XST = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        XST.append([x, s, t])
    D = []
    for _ in range(Q):
        D.append(II())
    ans = [-1] * len(D)
    XST.sort()
    for x, s, t in XST:
        s0 = bisect_left(D, s)
        t0 = bisect_left(D, t)
        for i in range(s0, t0):
            if ans[i] == -1:
                ans[i] = x
    for y in ans:
        print(y)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq
from operator import itemgetter
N, Q = map(int, input().split())
Stop = []
for i in range(N):
    S, T, X = map(int, input().split())
    Stop.append([S-X, X, True, i])
    Stop.append([T-X, X, False, i])
Stop.sort(key=itemgetter(0))
q = []
notClosed = [False]*N
for _ in range(Q):
    D = int(input())
    i = 0
    while i < len(Stop) and Stop[i][0] <= D:
        time, pos, lr, idx = Stop[i]
        if lr:
            heapq.heappush(q, (pos, idx))
        else:
            notClosed[idx] = True
            while len(q)>0 and notClosed[q[0][1]]:
                aaa = heapq.heappop(q)[1]
        i+=1
    if len(q):
        print(q[0][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_right

input = sys.stdin.readline

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

e = []
for _ in range(N):
    S, T, X = map(int, input().split())
    e.append([S - X, 1, X])
    e.append([T - X, -1, X])
e.sort()

s1, s2 = [], []
tmp = set()
for t, p, x in e:
    if p == 1:
        tmp.add(x)
    else:
        tmp.remove(x)
    s1.append(t)
    if len(tmp) > 0:
        s2.append(min(tmp))
    else:
        s2.append(-1)

n = len(s2)
for i in range(Q):
    D = int(input())
    i = bisect_right(s1, D)
    ans = -1
    if 0 <= i - 1 < n:
        ans = s2[i - 1]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math
import heapq


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


# n : kouji count
def main():
    n, q = intin()
    stxlist = intinl(n)
    dlist = intinl(q)

    stxlist = [(s - x, t - x, x) for s, t, x in stxlist]

    event_list = []
    for s, t, x in stxlist:
        event_list.append((s, 1, x))
        event_list.append((t, -1, x))

    for d in dlist:
        event_list.append((d, 2, 0))

    event_list.sort()

    stop_list = []
    remove_list = []
    for time, event, x in event_list:
        if event == 2:
            if stop_list:
                print(stop_list[0])
            else:
                print(-1)
        if event == 1:
            heapq.heappush(stop_list, x)
        if event == -1:
            heapq.heappush(remove_list, x)
            while remove_list and stop_list and (remove_list[0] == stop_list[0]):
                heapq.heappop(remove_list)
                heapq.heappop(stop_list)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq
from operator import itemgetter
N, Q = map(int, input().split())
Stop = []
for i in range(N):
    S, T, X = map(int, input().split())
    Stop.append([S-X, X, 0, i])
    Stop.append([T-X, X, 1, i])
Stop.sort(key=itemgetter(0))
q = []
notClosed = [False]*N
for _ in range(Q):
    D = int(input())
    i = 0
    while i < len(Stop) and Stop[i][0] <= D:
        time, pos, lr, idx = Stop[i]
        if lr == 0:
            heapq.heappush(q, (pos, idx))
        else:
            notClosed[idx] = True
            while len(q)>0 and notClosed[q[0][1]]:
                aaa = heapq.heappop(q)[1]
        i+=1
    if len(q):
        print(q[0][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from bisect import bisect_left

def main():
    N, Q = LI()
    XST = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        XST.append([x, s, t])
    D = []
    for _ in range(Q):
        D.append(II())
    ans = [-1] * len(D)
    XST.sort(reverse=True)
    for x, s, t in XST:
        s0 = bisect_left(D, s)
        t0 = bisect_left(D, t)
        for i in range(s0, t0):
            ans[i] = x
    for y in ans:
        print(y)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
n,Q = list(map(int, input().split()))
Y=[]
for j in range(n):
    a=list(map(int, input().split()))
    Y.append([a[0]-a[2],a[1]-a[2],a[2]])
q = [int(input()) for i in range(Q)]
Y.sort()
X=[]
p=0
for d in q:
    while p<n and Y[p][0]<=d:
        heappush(X,(Y[p][2],Y[p][1]))
        p+=1
    while X and X[0][1] <= d:
        heappop(X)
    if X:
        print(X[0][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq
from operator import itemgetter
N, Q = map(int, input().split())
Stop = []
for i in range(N):
    S, T, X = map(int, input().split())
    Stop.append([S-X, X, 0, i])
    Stop.append([T-X, X, 1, i])
Stop.sort(key=itemgetter(0))
q = []
notClosed = [False]*N
for _ in range(Q):
    D = int(input())
    i = 0
    while i < len(Stop) and Stop[i][0] <= D:
        time, pos, lr, idx = Stop[i]
        if lr == 0:
            heapq.heappush(q, (pos, idx))
        else:
            notClosed[idx] = True
            while len(q) and notClosed[q[0][1]]:
                aaa = heapq.heappop(q)[1]
        i+=1
    if len(q):
        print(q[0][0])
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, Q = LI()
    events = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        events.append([s, x])
        events.append([t, -x])
    for _ in range(Q):
        d = II()
        events.append([d, INF])
    events.sort()
    dst = set()
    ans = []
    for t, x in events:
        if x == INF: # query
            y = min(dst) if dst else -1
            ans.append(y)
        elif x > 0: # add
            dst.add(x)
        else: # x < 0, delete
            dst.remove(-x)
    print('\n'.join(map(str, ans)))
    return

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function
 
import collections
import os
import sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()
 
 
def main():
    N, Q = [int(x) for x in input().split()]
    updates = []
    for i in range(N):
        S, T, X = [int(x) for x in input().split()]
        updates.append((S - X, 1, X))
        updates.append((T - X, -1, X))
 
    active = []
    to_delete = collections.defaultdict(int)
    import heapq
    heapq.heapify(updates)
    g = lambda *a:a
    updates = [g(*k) for k in updates]
 
    for _ in range(Q):
        x = int(input())
        while updates and updates[0][0] <= x:
            coord, tp, val = heapq.heappop(updates)
 
            if tp == -1:
                to_delete[val] += 1
            else:
                heapq.heappush(active, val)
 
            while active and to_delete[active[0]] > 0:
                to_delete[active[0]] -= 1
                heapq.heappop(active)
        sys.stdout.write(str(active[0]) if active else '-1')
        sys.stdout.write('\n')
 
# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._buffer = BytesIO()
        self._fd = file.fileno()
        self._writable = "x" in file.mode or "r" not in file.mode
        self.write = self._buffer.write if self._writable else None
 
    def read(self):
        if self._buffer.tell():
            return self._buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()
 
    def flush(self):
        if self._writable:
            os.write(self._fd, self._buffer.getvalue())
            self._buffer.truncate(0), self._buffer.seek(0)
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = sys.stdin.readline
# endregion
 
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function
 
import collections
import os
import sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()
 
 
def main():
    N, Q = [int(x) for x in input().split()]
    updates = []
    for i in range(N):
        S, T, X = [int(x) for x in input().split()]
        updates.append((S - X, 1, X))
        updates.append((T - X, -1, X))
 
    active = []
    to_delete = collections.defaultdict(int)
    import heapq
    heapq.heapify(updates)
    g = lambda *a:a
    updates = [g(*k) for k in updates]

    for _ in range(Q):
        x = int(input())
        while updates and updates[0][0] <= x:
            coord, tp, val = heapq.heappop(updates)
 
            if tp == -1:
                to_delete[val] += 1
            else:
                heapq.heappush(active, val)
 
            while active and to_delete[active[0]] > 0:
                to_delete[active[0]] -= 1
                heapq.heappop(active)
        print(active[0] if active else -1)
 
 
# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._buffer = BytesIO()
        self._fd = file.fileno()
        self._writable = "x" in file.mode or "r" not in file.mode
        self.write = self._buffer.write if self._writable else None
 
    def read(self):
        if self._buffer.tell():
            return self._buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()
 
    def flush(self):
        if self._writable:
            os.write(self._fd, self._buffer.getvalue())
            self._buffer.truncate(0), self._buffer.seek(0)
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")
 
# endregion
 
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, Q, STX, D):
    E = []
    for i, (s, t, x) in enumerate(STX):
        E.append((s-x, 1, x, i))
        E.append((t-x, -1, x, i))

    E.sort(reverse=True)

    q = []
    done = set()
    D = [(i, d) for i, d in enumerate(D)]
    D.sort(reverse=True)
    ans = [-1] * Q
    while D:
        while E and E[-1][0] <= D[-1][1]:
            t, e, x, i = E.pop()
            if e == 1:
                heapq.heappush(q, (x, i))
            else:
                done.add((x, i))

        while q and q[0] in done:
            heapq.heappop(q)

        i, d = D.pop()
        if q and q[0][0] >= d:
            ans[i] = q[0][0]

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


def main():
    N, Q = read_int_n()
    STX = [read_int_n() for _ in range(N)]
    D = [read_int() for _ in range(Q)]
    (slv(N, Q, STX, D))

    N = 2 * (10**5)
    Q = 2 * (10**5)
    STX = []
    for _ in range(N):
        s = random.randint(0, 10**9)
        t = random.randint(0, 10**9)
        if s > t:
            s, t = t, s
        x = random.randint(0, 10**9)
        STX.append((s, t, x))
    D = [random.randint(0, 10**9) for _ in range(Q)]
    (slv(N, Q, STX, D))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int,input().split())
List_S = [list(map(int,input().split())) for i in range(N)]
List_D = [int(input()) for i in range(Q)]

List_S.sort(key=lambda x:x[0])
List_S.sort(key=lambda x:x[2])

n = N

for i in range(Q):
    flag = 0
    count = 0
    for j in range(n):
        if List_S[0][1] - 0.5 < (List_D[i] + List_S[0][2]):
            del List_S[0]
            n -= 1
        else:
            break
    for k in range(n):            
        if (List_S[k][0] - 0.5 < (List_D[i] + List_S[k][2])) and ((List_D[i] + List_S[k][2]) < List_S[k][1]-0.5):
            print("{}".format(List_S[k][2]))
            flag = 1
            break
    if flag == 0:
        print("-1")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, Q = LI()
    events = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        events.append([s, x])
        events.append([t, -x])
    for _ in range(Q):
        d = II()
        events.append([d, INF])
    events.sort()
    dst = set()
    for t, x in events:
        if x == INF: # query
            y = min(dst) if dst else -1
            print(y)
        elif x > 0: # add
            dst.add(x)
        else: # x < 0, delete
            dst.remove(-x)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N, Q = LI()
    events = []
    for _ in range(N):
        s, t, x = LI()
        s -= x
        t -= x
        events.append([s, x])
        events.append([t, -x])
    for _ in range(Q):
        d = II()
        events.append([d, INF])
    events.sort()
    dst = {10 ** 10}
    for t, x in events:
        if x == INF: # query
            y = min(dst)
            if y == 10 ** 10:
                y = -1
            print(y)
        elif x > 0: # add
            dst.add(x)
        else: # x < 0, delete
            dst.remove(-x)
    return

main()

Submission status: Time Limit Exceeded

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

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

STX = [[int(x) for x in input().split()] for _ in range(N)]
for s,t,x in STX:
  heappush(q,(s-x,1,x)) # xで死ぬ
  heappush(q,(t-x,0,x)) # xで死ななくなる
  
D = [int(input()) for _ in range(Q)]
for i,d in enumerate(D):
  heappush(q,(d,2,i))
answer = [-1]*Q

se = set() # 引っかかってる場所

while q:
  a,c,b = heappop(q)
  if not c:
    se.remove(b)
  elif c&1:
    se.add(b)
  else:
    if se:
      answer[b] = min(se)

print('\n'.join(map(str,answer)))