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:
import sys
import bisect
 
N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]
 
G.sort(key = lambda x : -x[2])

class SegTree :
    def __init__(self, n) :
        self.n = n
        self.A = [None] * (2 << (n + 1).bit_length())
 
    def update(self, i, l, r, ll, rr, val) :
        if r <= ll or rr <= l :
            return
        if ll <= l and r <= rr :
            self.A[i] = val
            return
 
        if self.A[i] != None :
            self.A[i * 2] = self.A[i * 2 + 1] = self.A[i]
            self.A[i] = None
 
        m = (l + r) // 2
        self.update(i * 2, l, m, ll, rr, val)
        self.update(i * 2 + 1, m, r, ll, rr, val)
 
    def get(self, i, l, r, p) :
        if self.A[i] != None or l + 1 == r :
            return self.A[i]
        m = (l+r) // 2
        if p < m :
            return self.get(i * 2, l, m, p)
        else :
            return self.get(i * 2 + 1, m, r, p)
 
ST = SegTree(Q)
for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue
 
    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)
 
    if ll == rr :
        continue
 
    ST.update(1, 0, Q, ll, rr, g[2])
 
for i in range(Q) :
    ans = ST.get(1, 0, Q, i)
    if ans == None :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
 
N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]
 
G.sort(key = lambda x : -x[2])
T.sort()
 
class SegTree :
    def __init__(self, n) :
        self.n = n
        self.A = [None] * (2 << (n + 1).bit_length())
 
    def update(self, i, l, r, ll, rr, val) :
        if r <= ll or rr <= l :
            return
        if ll <= l and r <= rr :
            self.A[i] = val
            return
 
        if self.A[i] != None :
            self.A[i * 2] = self.A[i * 2 + 1] = self.A[i]
            self.A[i] = None
 
        m = (l + r) // 2
        self.update(i * 2, l, m, ll, rr, val)
        self.update(i * 2 + 1, m, r, ll, rr, val)
 
    def get(self, i, l, r, p) :
        if self.A[i] != None or l + 1 == r :
            return self.A[i]
        m = (l+r) // 2
        if p < m :
            return self.get(i * 2, l, m, p)
        else :
            return self.get(i * 2 + 1, m, r, p)
 
ST = SegTree(Q)
for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue
 
    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)
 
    if ll == rr :
        continue
 
    ST.update(1, 0, Q, ll, rr, g[2])
 
for i in range(Q) :
    ans = ST.get(1, 0, Q, i)
    if ans == None :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]

G.sort(key = lambda x : -x[2])
T.sort()

A = [0] * (4 * Q + 5)

def update(A, i, l, r, ll, rr, val) :
    if r <= ll or rr <= l :
        return
    if ll <= l and r <= rr :
        A[i] = val
        return

    if A[i] != 0 :
        A[i * 2] = A[i * 2 + 1] = A[i]
        A[i] = 0

    m = (l + r) // 2
    update(A, i * 2, l, m, ll, rr, val)
    update(A, i * 2 + 1, m, r, ll, rr, val)

def get(A, i, l, r, p) :
    if A[i] != 0 or l + 1 == r :
        return A[i]
    m = (l+r) // 2
    if p < m :
        return get(A, i * 2, l, m, p)
    else :
        return get(A, i * 2 + 1, m, r, p)

for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue

    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)

    if ll == rr :
        continue

    update(A, 1, 0, Q, ll, rr, g[2])

for i in range(Q) :
    ans = get(A, 1, 0, Q, i)
    if ans == 0 :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]

G.sort(key = lambda x : -x[2])
T.sort()


A = [-2] * (4 * Q + 5)

def update(i, l, r, ll, rr, val) :
    if r <= ll or rr <= l :
        return
    if ll <= l and r <= rr :
        A[i] = val
        return

    if A[i] != -2 :
        A[i * 2] = A[i * 2 + 1] = A[i]
        A[i] = -2

    m = (l + r) // 2
    update(i * 2, l, m, ll, rr, val)
    update(i * 2 + 1, m, r, ll, rr, val)

def get(i, l, r, p) :
    if A[i] != -2 or l + 1 == r :
        return A[i]
    m = (l+r) // 2
    if p < m :
        return get(i * 2, l, m, p)
    else :
        return get(i * 2 + 1, m, r, p)

for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue

    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)

    if ll == rr :
        continue

    update(1, 0, Q, ll, rr, g[2])

for i in range(Q) :
    ans = get(1, 0, Q, i)
    if ans == -2 :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]

G.sort(key = lambda x : -x[2])
T.sort()


class SegTree :
    def __init__(self, n) :
        self.n = n
        self.A = [-2] * (4 * n + 5)

    def update(self, i, l, r, ll, rr, val) :
        if r <= ll or rr <= l :
            return
        if ll <= l and r <= rr :
            self.A[i] = val
            return

        if self.A[i] != -2 :
            self.A[i * 2] = self.A[i * 2 + 1] = self.A[i]
            self.A[i] = -2

        m = (l + r) // 2
        self.update(i * 2, l, m, ll, rr, val)
        self.update(i * 2 + 1, m, r, ll, rr, val)

    def get(self, i, l, r, p) :
        if self.A[i] != -2 or l + 1 == r :
            return self.A[i]
        m = (l+r) // 2
        if p < m :
            return self.get(i * 2, l, m, p)
        else :
            return self.get(i * 2 + 1, m, r, p)

ST = SegTree(Q)
for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue

    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)

    if ll == rr :
        continue

    ST.update(1, 0, Q, ll, rr, g[2])

for i in range(Q) :
    ans = ST.get(1, 0, Q, i)
    if ans == -2 :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

N, Q = map(int, input().split(' '))
G = [list(map(int, sys.stdin.readline().split(' '))) for _ in range(N)]
T = [int(sys.stdin.readline()) for _ in range(Q)]

G.sort(key = lambda x : -x[2])
T.sort()

class SegTree :
    def __init__(self, n) :
        self.n = n
        self.A = [None] * (4 * n + 5)

    def update(self, i, l, r, ll, rr, val) :
        if r <= ll or rr <= l :
            return
        if ll <= l and r <= rr :
            self.A[i] = val
            return

        if self.A[i] != None :
            self.A[i * 2] = self.A[i * 2 + 1] = self.A[i]
            self.A[i] = None

        m = (l + r) // 2
        self.update(i * 2, l, m, ll, rr, val)
        self.update(i * 2 + 1, m, r, ll, rr, val)

    def get(self, i, l, r, p) :
        if self.A[i] != None or l + 1 == r :
            return self.A[i]
        m = (l+r) // 2
        if p < m :
            return self.get(i * 2, l, m, p)
        else :
            return self.get(i * 2 + 1, m, r, p)

ST = SegTree(Q)
for g in G :
    s = max(0, g[0] - g[2])
    e = g[1] - g[2]
    if e <= 0 :
        continue

    ll = bisect.bisect_left(T, s)
    rr = bisect.bisect_left(T, e)

    if ll == rr :
        continue

    ST.update(1, 0, Q, ll, rr, g[2])

for i in range(Q) :
    ans = ST.get(1, 0, Q, i)
    if ans == None :
        print(-1)
    else :
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
n, Q = [int(x) for x in input().split()]
stx = [[int(x) for x in input().split()] for i in range(n)]
q = [int(input()) for i in range(Q)]
stx.sort(key=lambda x: x[2])
ans = [-1 for i in range(Q)]
skip = [-1 for i in range(Q)]

for (s, t, x) in stx:
  begin = bisect_left(q, s-x)
  end = bisect_left(q, t-x)
  while begin<end:
    if skip[begin]==-1:
      ans[begin] = x
      skip[begin] = end
      begin += 1
    else:
      begin = skip[begin]
for e in ans:
  print(e)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l>>=1
            r>>=1
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
n, Q = list(map(int, input().split()))
stx = [list(map(int, input().split())) for i in range(n)]
q = [int(input()) for i in range(Q)]
stx.sort(key=lambda x: x[2])
ans = [-1 for i in range(Q)]
skip = [-1 for i in range(Q)]

for (s, t, x) in stx:
  begin = bisect_left(q, s-x)
  end = bisect_left(q, t-x)
  while begin<end:
    if skip[begin]==-1:
      ans[begin] = x
      skip[begin] = end
      begin += 1
    else:
      begin = skip[begin]
for e in ans:
  print(e)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                data[r-2]=x
                r=(r-1)//2
            else:
                r//=2
            if l&1:
                data[l-1]=x
                l=(l+1)//2
            else:
                l//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:r-=1;data[r-1]=x
            if l&1:data[l-1]=x;l+=1
            l>>=1;r>>=1
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
n, Q = list(map(int, input().split()))
stx = [list(map(int, input().split())) for i in range(n)]
q = [int(input()) for i in range(Q)]
stx.sort(key=lambda x: x[2])
ans = [-1 for i in range(Q)]

for (s, t, x) in stx:
  begin = bisect_left(q, s-x)
  end = bisect_left(q, t-x)
  for i in range(begin, end):
    if ans[i]==-1:
      ans[i] = x
for e in ans:
  print(e)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l>>=1
            r>>=1
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        k=[k]
        [k.append((i-1)//2)for i in k if i]
        for k in k:
            if data[k]<s:s=data[k]
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r%2==1:
                r-=1
                data[r-1]=x
            if l%2==1:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r%2:
                r-=1
                data[r-1]=x
            if l%2:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

def solve():
    N, Q = map(int, input().split())
    events = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        events.append((s-x, 1, x))
        events.append((t-x, -1, x))
    for _ in range(Q):
        d = int(input())
        events.append((d, 2, d))
    events = sorted(events)

    constructions = []
    under_construction = defaultdict(bool)
    ans = ''
    for _, flag, coord in events:
        if flag == 1:
            heappush(constructions, coord)
            under_construction[coord] = True
        elif flag == -1:
            under_construction[coord] = False
        else:
            while constructions:
                x = heappop(constructions)
                if under_construction[x]: # x is the minimum coordinate under construction
                    heappush(constructions, x)
                    ans += str(x) + '\n'
                    break
            else: # no constructions
                ans += '-1\n'
    print(ans)
    
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

N, Q = map(int, input().split())
events = []
for _ in range(N):
    s, t, x = map(int, input().split())
    events.append((s-x, 1, x))
    events.append((t-x, -1, x))
for _ in range(Q):
    d = int(input())
    events.append((d, 2, d))
events = sorted(events)

constructions = []
under_construction = defaultdict(bool)
ans = ''
for _, flag, coord in events:
    if flag == 1:
        heappush(constructions, coord)
        under_construction[coord] = True
    elif flag == -1:
        under_construction[coord] = False
    else:
        while constructions:
            x = heappop(constructions)
            if under_construction[x]: # x is the minimum coordinate under construction
                heappush(constructions, x)
                ans += str(x) + '\n'
                break
        else: # no constructions
            ans += '-1\n'
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, Q = list(map(int, input().split()))
ans = [-1 for i in range(Q)]
event = []
for i in range(n):
  s, t, x = list(map(int, input().split()))
  event.append((s-x, 1, x))
  event.append((t-x, 0, x))
for i in range(Q):
  q = int(input())
  event.append((q, 2, i))
event.sort()
stop = set()
min_s = 1e10
for (t, q, x) in event:
  if q==1:
    stop.add(x)
    min_s = min(min_s, x)
  elif q==0:
    stop.remove(x)
    if len(stop):
      min_s = min(stop)
    else:
      min_s = 1e10
  else:
    if min_s != 1e10:
      ans[x] = min_s
for e in ans:
  print(e)

Submission status: Time Limit Exceeded

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

N, Q = map(int, input().split())
events = []
for _ in range(N):
    s, t, x = map(int, input().split())
    events.append((s-x, 1, x))
    events.append((t-x, -1, x))
events = deque(sorted(events))
D = [int(input()) for _ in range(Q)]

constructions = []
under_construction = defaultdict(bool)
ans = ''
for d in D:
    while events:
        if events[0][0] > d:
            break
        _, flag, coord = events.popleft()            
        if flag > 0:
            heappush(constructions, coord)
            under_construction[coord] = True
        else:
            under_construction[coord] = False
    while constructions:
        x = heappop(constructions)
        if under_construction[x]: # x is the minimum coordinate under construction
            heappush(constructions, x)
            ans += str(x) + '\n'
            break
    else: # no constructions
        ans += '-1\n'
print(ans)

Submission status: Time Limit Exceeded

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

N, Q = map(int, input().split())
events = []
for _ in range(N):
    s, t, x = map(int, input().split())
    events.append((s-x, 1, x))
    events.append((t-x, -1, x))
events = deque(sorted(events))
D = []
for _ in range(Q):
    D.append(int(input()))

constructions = []
under_construction = defaultdict(bool)
ans = ''
for d in D:
    while events:
        if events[0][0] > d:
            break
        _, flag, coord = events.popleft()            
        if flag > 0:
            heappush(constructions, coord)
            under_construction[coord] = True
        else:
            under_construction[coord] = False
    while constructions:
        x = heappop(constructions)
        if under_construction[x]: # x is the minimum coordinate under construction
            heappush(constructions, x)
            ans += str(x) + '\n'
            break
    else: # no constructions
        ans += '-1\n'
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print('\n'.join(map(str,a)))
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

N, Q = map(int, input().split())
events = []
for _ in range(N):
    s, t, x = map(int, input().split())
    events.append((s-x, 1, x))
    events.append((t-x, -1, x))
events = deque(sorted(events))
D = []
for _ in range(Q):
    D.append(int(input()))

constructions = []
under_construction = defaultdict(bool)
i = 0
for d in D:
    while events:
        if events[0][0] > d:
            break
        _, flag, coord = events.popleft()            
        if flag > 0:
            heappush(constructions, coord)
            under_construction[coord] = True
        else:
            under_construction[coord] = False
    while constructions:
        x = heappop(constructions)
        if under_construction[x]: # x is the minimum coordinate under construction
            heappush(constructions, x)
            print(x)
            break
    else: # no constructions
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    a=[]
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        a.append(-(s==I)or s)
    print(*a)
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    from collections import defaultdict
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=defaultdict(lambda:I)
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        print(-(s==I)or s)
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

def doevent(lis):
    global stop
    a,b=lis
    if b>=0:
        if stop=={-1}:
            stop=set()
        stop.add(b)
    else:
        if -b in stop:
            stop.remove(-b)
        if stop==set():
            stop={-1}
    return min(stop)

stop=set([-1])
def main():
    n,q=map(int,input().split())
    event=[]
    
    for i in range(n):
        s,t,x=map(int,input().split())
        event.append((s-x,x))
        event.append((t-x,-x))
    event.sort(key=lambda x:x[0])

    minlist=defaultdict(int)

    for k,v in enumerate(event):
        minlist[k]=doevent(v)

    for i in range(q):
        sys.stdout.writelines(str(minlist[bisect_right(event,(int(input()),10**9))-1])+"\n")
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,l,r in sorted(((x,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for s,t,x in zip(t[::3],t[1::3],t[2::3])),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l//=2
            r//=2
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        print(-(s==I)or s)
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# [時間、変数、処理の種類]をイベントに追加。処理の種類は1：工事開始、2：工事終了、3：判定
n, q = map(int, input().split())
events = []
ans = [-1] * q
koji = set()
for _ in range(n):
    s, t, x = map(int, input().split())
    events += [[s - x - 0.3, x, 1]]
    events += [[t - x - 0.7, x, 2]]
for i in range(q):
    d = int(input())
    events += [[d, i, 3]]
events.sort()
for j, h, s in events:
    if s == 1:
        koji.add(h)
    elif s == 2:
        koji.remove(h)
    else:
        if koji:
            ans[h] = min(koji)
print(*ans, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
# [時間、変数、処理の種類]をイベントに追加。処理の種類は1：工事開始、2：工事終了、3：判定
from heapq import *

n, q = map(int, input().split())
events = []
ans = [-1] * q
koji = set()
for _ in range(n):
    s, t, x = map(int, input().split())
    events += [[s - x - 0.3, x, 1]]
    events += [[t - x - 0.7, x, 2]]
for i in range(q):
    d = int(input())
    events += [[d, i, 3]]
heapify(events)
cnt=0
for _ in range(n * 2 + q):
    j, h, s = heappop(events)
    if s == 1:
        koji.add(h)
    elif s == 2:
        koji.remove(h)
    else:
        if koji:
            ans[h] = min(koji)
        cnt+=1
    if cnt==q:
        break
print(*ans, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
def main():
    from bisect import bisect_left
    I=10**9+1
    n,q,*t=map(int,open(0).read().split())
    d=tuple(t[-q:])
    t=t[:-q]
    N0=2**(q-1).bit_length()
    data=[I]*2*N0
    for x,_,l,r in sorted(((x,_,bisect_left(d,s-x)+N0,bisect_left(d,t-x)+N0)for _,(s,t,x)in enumerate(zip(t[::3],t[1::3],t[2::3]))),reverse=1):
        while l<r:
            if r&1:
                r-=1
                data[r-1]=x
            if l&1:
                data[l-1]=x
                l+=1
            l>>=1
            r>>=1
    for k in range(N0-1,q+N0-1):
        s=I
        while~k:
            if data[k]<s:s=data[k]
            k=(k-1)//2
        print(-(s==I)or s)
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
V = [list(map(int,inputs().split())) for p in [0]*N]
M = [int(inputs()) for p in [0]*Q]
V.sort(key = lambda x:(x[2]))

for m in M:
	for v in V:
		if v[0] - v[2] <=m and m <= v[1] - v[2] -1:
			print(v[2])
			break
	else:
		print(-1)

Submission status: Time Limit Exceeded

Solution code:
# [時間、変数、処理の種類]をイベントに追加。処理の種類は1：工事開始、2：工事終了、3：判定
from heapq import *

n, q = map(int, input().split())
events = []
ans = [-1] * q
koji = set()
for _ in range(n):
    s, t, x = map(int, input().split())
    events += [[s - x - 0.3, x, 1]]
    events += [[t - x - 0.7, x, 2]]
for i in range(q):
    d = int(input())
    events += [[d, i, 3]]
heapify(events)
for _ in range(n * 2 + q):
    j, h, s = heappop(events)
    if s == 1:
        koji.add(h)
    elif s == 2:
        koji.remove(h)
    else:
        if koji:
            ans[h] = min(koji)
print(*ans, sep="\n")

Submission status: Time Limit Exceeded

Solution code:
import sys
inputs = sys.stdin.readline
N, Q= map(int,inputs().split())
V = [list(map(int,inputs().split())) for p in [0]*N]

for q in [0]*Q:
	ans = sys.maxsize
	m = int(inputs())
	for v in V:
		if v[0] - v[2] <=m and m <= v[1] - v[2] -1:
			ans = min(v[2],ans)
	if ans < sys.maxsize:
		print(ans)
	else:
		print(-1)

Submission status: Time Limit Exceeded

Solution code:
# [時間、変数、処理の種類]をイベントに追加。処理の種類は1：工事開始、2：工事終了、3：判定
from heapq import *

n, q = map(int, input().split())
events = []
heapify(events)
ans = [-1] * q
koji = set()
for _ in range(n):
    s, t, x = map(int, input().split())
    heappush(events, [s - x - 0.3, x, 1])
    heappush(events, [t - x - 0.7, x, 2])
for i in range(q):
    d = int(input())
    heappush(events, [d, i, 3])
for _ in range(n * 2 + q):
    j, h, s = heappop(events)
    if s == 1:
        koji.add(h)
    elif s == 2:
        koji.remove(h)
    else:
        if koji:
            ans[h] = min(koji)
print(*ans,sep="\n")

Submission status: Time Limit Exceeded

Solution code:
def solve():
    from heapq import heappush, heappop, merge
    from collections import defaultdict

    N, Q = map(int, input().split())
    events = []
    for _ in range(N):
        s, t, x = map(int, input().split())
        events.append((s-x, 1, x))
        events.append((t-x, -1, x))
    events = sorted(events)

    constructions = []
    under_construction = defaultdict(bool)
    i = 0
    for _ in range(Q):
        d = int(input())
        while i < 2*N and events[i][0] <= d:
            if events[i][1] > 0:
                heappush(constructions, events[i][2])
                under_construction[events[i][2]] = True
            else:
                under_construction[events[i][2]] = False
            i += 1
        while constructions:
            x = heappop(constructions)
            if under_construction[x]: # x is the minimum coordinate under construction
                heappush(constructions, x)
                print(x)
                break
        else: # no constructions
            print(-1)

if __name__ == "__main__":
    solve()