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: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()

class SegmentTree(object):
    def __init__(self,A,dot,e,comp,id,act):
        n=2**((len(A)-1).bit_length())
        self.__n=n
        self.__dot=dot
        self.__e=e
        self.__comp=comp
        self.__id=id
        self.__act=act
        self.__node=[e]*(2*n)
        self.__lazy=[id]*(2*n)
        for i in range(len(A)):
            self.__node[i+n]=A[i]
        for i in range(n-1,0,-1):
            self.__node[i]=self.__dot(self.__node[2*i],self.__node[2*i+1])

    def __propagate(self,i):
        if(self.__lazy[i]==self.__id): return
        node=self.__node
        lazy=self.__lazy
        if(i<self.__n): # propagate to children
            lazy[2*i]=self.__comp(lazy[2*i],lazy[i])
            lazy[2*i+1]=self.__comp(lazy[2*i+1],lazy[i])
        node[i]=self.__act(lazy[i],node[i]) # action
        lazy[i]=self.__id

    def __ancestors_propagate(self,i):
        if(i==1): return
        i//=2
        self.__ancestors_propagate(i)
        self.__propagate(i)

    def __update_ancestors(self,i):
        while(i!=1):
            self.__propagate(i+1-2*(i%2)) # propagate the sibling of i
            i//=2
            self.__node[i]=self.__dot(self.__node[2*i],self.__node[2*i+1])

    def update(self,i,c):
        i+=self.__n
        self.__ancestors_propagate(i)
        self.__propagate(i)
        self.__node[i]=c
        self.__update_ancestors(i)

    def add(self,l,r,f):
        range,low=[],[0]*2
        l+=self.__n; r+=self.__n
        while(l<r):
            if(l%2==1):
                if(low[0]==0): low[0]=l
                range.append(l)
                l+=1
            l//=2
            if(r%2==1):
                r-=1
                range.append(r)
                if(low[1]==0): low[1]=r
            r//=2
        for i in low:
            if(i): self.__ancestors_propagate(i)
        for i in range: self.__lazy[i]=self.__comp(self.__lazy[i],f)
        for i in low:
            if(i):
                self.__propagate(i)
                self.__update_ancestors(i)

    def sum(self,l,r):
        low=[0]*2
        vl,vr=self.__e,self.__e
        l+=self.__n; r+=self.__n
        while(l<r):
            if(l%2==1):
                if(low[0]==0):
                    self.__ancestors_propagate(l)
                    low[0]=1
                self.__propagate(l)
                vl=self.__dot(vl,self.__node[l])
                l+=1
            l//=2
            if(r%2==1):
                r-=1
                if(low[1]==0):
                    self.__ancestors_propagate(r)
                    low[1]=1
                self.__propagate(r)
                vr=self.__dot(self.__node[r],vr)
            r//=2
        return self.__dot(vl,vr)

def resolve():
    n,q=map(int,input().split())
    STX=[tuple(map(int,input().split())) for _ in range(n)]
    STX.sort()
    D=[int(input()) for _ in range(q)]

    # 座標圧縮
    S=set([max(0,s-x) for s,t,x in STX]+[max(0,t-x) for s,t,x in STX]+D)
    P={x:i for i,x in enumerate(sorted(S))}
    n=len(P)

    tree=SegmentTree([INF]*n,min,INF,min,INF,min)
    for i in range(len(STX)):
        s,t,x=STX[i]
        u,v=P[max(s-x,0)],P[max(t-x,0)]
        tree.add(u,v,i)

    for d in D:
        p=P[d]
        i=tree.sum(p,p+1)
        if(i==INF): print(-1)
        else: print(STX[i][2])
resolve()

Submission status: Wrong Answer

Solution code:
def main():
    class segtree():
        def __init__(self,basesize):#self,モノイド,元の配列
            #ini：上限（下限）,base：元の配列,monoid：モノイド,tree：セグ木,depth：treeの深さ,
            #basesize：baseの要素数,treesize：treeの要素数,num：treesize-basesize
            
            # 初期値（上限とか下限）を決定
            self.ini=10**9+1
            #セグ木の要素数（num）および深さ（depth）を計算
            self.basesize=basesize
            self.num,self.depth=1,1
            while self.num<self.basesize:
                self.num*=2
                self.depth+=1
            self.num-=1
            #関数（モノイド、今回はmin）を設定
            self.monoid=min
            #セグ木を構築
            self.treesize=self.num+self.basesize
            self.tree=[self.ini]*self.treesize
    
        #半開区間[lower,upper)について更新の準備をする関数（テスト中、minやmaxのときのみ）
        def range_update_pre(self,lower,upper,new_value):
            q=[[0,1]]  #インデックス、階層
            while q:
                i,f=q.pop()
                #popしたインデックスと階層から、求める下限と上限を算出する
                #幅
                width=pow(2,self.depth-f)
                #下限と中央と上限
                kagen=(i-pow(2,f-1)+1)*width
                chuo=kagen+width//2
                jogen=kagen+width
                k=2*i+1
                if lower<=kagen and jogen<=upper:
                    self.tree[i]=self.monoid(new_value,self.tree[i])
                    continue
                if k<self.treesize:
                    if lower<=kagen and chuo<=upper:self.tree[k]=self.monoid(new_value,self.tree[k])
                    elif lower<=chuo:q.append([k,f+1])
                if k+1<self.treesize:
                    if lower<=chuo and jogen<=upper:self.tree[k+1]=self.monoid(new_value,self.tree[k+1])
                    elif chuo<=upper:q.append([k+1,f+1])

        #更新
        def range_update(self):
            for i in range(1,treesize):
                self.tree[i]=self.monoid(self.tree[i],self.tree[(i-1)//2])

    import bisect

    n,q=map(int,input().split())
    stx=[list(map(int,input().split())) for _ in [0]*n]
    d=[int(input()) for _ in [0]*q]
    stx=[[s-x,t-x-1,x] for s,t,x in stx]
    stx=[[bisect.bisect_left(d,s),bisect.bisect_right(d,t),x] for s,t,x in stx]
    seg=segtree(q)
    for s,t,x in stx:
        seg.range_update_pre(s,t,x)
    seg.range_update
    ans=[i for i in seg.tree[seg.treesize-seg.basesize:]]
    for i in ans:
        if i==10**9+1:
            print(-1)
        else:
            print(i)
main()

Submission status: Wrong Answer

Solution code:
def solve():
    from heapq import heappush, heappop
    from collections import defaultdict
    from operator import itemgetter
    import sys

    input = sys.stdin.readline

    inf = 1 << 31

    n, q = map(int, input().split())

    e = []  # 通行止めと解除にクエリ分割
    for _ in range(n):
        s, t, x = map(int, input().split())
        # 区間[s, t)に通行止め
        e.append((t - x, 0, x))  # 通行止め解除/0にすることで、解除と開始が同時刻の場合、先に解除される
        e.append((s - x, 1, x))  # 通行止め開始/1にすることで、同上

    for _ in range(q):
        d = int(input())
        e.append((d, 2, -1))  # 求値クエリ/2にすることで、通行止めの設定解除の反映後に到着する

    e.sort(key=itemgetter(0, 1))

    cnt = defaultdict(int)
    h = [inf]

    for time, type, x in e:
        if type == 0:
            if x == h[0]:
                heappop(h)
                while h and (cnt[h[0]] > 0):
                    heappop(h)
                    cnt[h[0]] -= 1
            else:
                cnt[x] += 1
        elif type == 1:
            heappush(h, x)
        else:
            print(-1 if h[0] == inf else h[0])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import namedtuple
    from operator import attrgetter

    Roadwork = namedtuple('Roadwork', 'start end x_coordinate')

    def stop_at(idx=0) -> 'function':
        work = None
        it = iter(e[idx:])
        j = -1

        def inner(d) -> int:
            nonlocal work
            nonlocal it
            nonlocal j
            if (work is not None) and (work.start <= d < work.end):
                return j, work.x_coordinate
            for j, work in enumerate(it, idx):
                if work.start <= d < work.end:
                    return j, work.x_coordinate
            return -1, -1

        return inner

    n, q = map(int, input().split())

    e = []
    for _ in range(n):
        s, t, x = map(int, input().split())
        e.append(Roadwork(start=s - x, end=t - x, x_coordinate=x))
    e.sort(key=attrgetter('x_coordinate'))

    idx = 0
    for _ in range(q):
        d = int(input())
        f = stop_at(idx)
        idx, ret = f(d)
        print(ret)


if __name__ == '__main__':
    main()

# たぶん、TLE

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque
import sys
input = sys.stdin.readline

n, q = map(int, input().split())
event = []
stx = []
for i in range(n):
    s, t, x = map(int, input().split())
    heappush(event, (s - 0.5 - x, +1, i))
    heappush(event, (t - 0.5 - x, -1, i))
    stx.append((s - x, t - x, x))

for i in range(q):
    d = int(input())
    heappush(event, (d, 0, i))

ans = [-1] * q
queue = []  # [(t, x)]
while event:
    d, k, i = heappop(event)
    # print(d, k, i, queue)
    if k == 0:
        if len(queue) > 0:
            ans[i] = queue[0][1]
    elif k == 1:
        heappush(queue, (stx[i][1], stx[i][2]))
    else:
        heappop(queue)

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque
import sys
input = sys.stdin.readline

n, q = map(int, input().split())
event = []
stx = []
for i in range(n):
    s, t, x = map(int, input().split())
    heappush(event, (s - 0.5 - x, +1, i))
    heappush(event, (t - 0.5 - x, -1, i))
    stx.append((s, t, x))

for i in range(q):
    d = int(input())
    heappush(event, (d, 0, i))

ans = [-1] * q
queue = []
while event:
    d, k, i = heappop(event)
    # print(d, k, i, queue)
    if k == 0:
        if len(queue) > 0:
            ans[i] = queue[0][1]
    elif k == 1:
        heappush(queue, (stx[i][1], stx[i][2]))
    else:
        heappop(queue)

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque
import sys
input = sys.stdin.readline

n, q = map(int, input().split())
event = []
stx = []
for i in range(n):
    s, t, x = map(int, input().split())
    heappush(event, (s - 0.5 - x, +1, i))
    heappush(event, (t - 0.5 - x, -1, i))
    stx.append((s, t, x))

for i in range(q):
    d = int(input())
    heappush(event, (d, 0, i))

ans = [-1] * q
queue = []
while event:
    d, k, i = heappop(event)
    print(d, k, i, queue)
    if k == 0:
        if len(queue) > 0:
            ans[i] = queue[0][1]
    elif k == 1:
        heappush(queue, (stx[i][1], stx[i][2]))
    else:
        heappop(queue)

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

Submission status: Wrong Answer

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

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())
INF = float('inf')

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
    print(tree.data[0])

Submission status: Wrong Answer

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

STX = [tuple(map(int, input().split())) + (i, ) for i in range(N)]
D = [int(input()) for _ in range(Q)]  # D1 < D2 < ...

minStopS = [None] * N  # 止めれる最小のi
maxStopT = [None] * N  # 止めれる最大のi

STX.sort(key=lambda A: A[0])
now = 0
for s, _, x, i in STX:
    while now < Q and D[now] < s - x:
        now += 1
    minStopS[i] = now - 1

STX.sort(key=lambda A: A[1], reverse=True)
now = Q - 1
for _, t, x, i in STX:
    while now >= 0 and t - x <= D[now]:
        now -= 1
    maxStopT[i] = now

ans = [float('inf')] * Q
STX.sort(key=lambda A: A[2], reverse=True)
for _, _, x, i in STX:
    for q in range(minStopS[i], maxStopT[i]):
        ans[q] = x

for a in ans:
    print(-1 if a == float('inf') else a)

Submission status: Wrong Answer

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

STX = [tuple(map(int, input().split())) + (i, ) for i in range(N)]
D = [int(input()) for _ in range(Q)]  # D1 < D2 < ...

minStopS = [None] * N  # 止めれる最小のi
maxStopT = [None] * N  # 止めれる最大のi

STX.sort(key=lambda A: A[0])
now = 0
for s, _, x, i in STX:
    while now < Q and D[now] < s - x:
        now += 1
    minStopS[i] = now

STX.sort(key=lambda A: A[1], reverse=True)
now = Q - 1
for _, t, x, i in STX:
    while now >= 0 and t - x <= D[now]:
        now -= 1
    maxStopT[i] = now

ans = [float('inf')] * Q
STX.sort(key=lambda A: A[2], reverse=True)
for _, _, x, i in STX:
    for q in range(minStopS[i], maxStopT[i] + 1):
        ans[q] = x

for a in ans:
    print(-1 if a == float('inf') else a)

Submission status: Wrong Answer

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

STX = [tuple(map(int, input().split())) + (i, ) for i in range(N)]
D = [int(input()) for _ in range(Q)]  # D1 < D2 < ...

minStopS = [N] * N  # 止めれる最小のi
maxStopT = [-1] * N  # 止めれる最大のi

STX.sort(key=lambda A: A[0])
now = 0
for s, _, x, i in STX:
    while now < Q and D[now] < s - x:
        now += 1
    minStopS[i] = now

STX.sort(key=lambda A: A[1], reverse=True)
now = Q - 1
for _, t, x, i in STX:
    while now >= 0 and t - x <= D[now]:
        now -= 1
    maxStopT[i] = now

ansS = [float('inf' )] * Q
ansT = [float('inf')] * Q
STX.sort(key=lambda A: A[2], reverse=True)

for _, _, x, i in STX:
    ansS[minStopS[i]] = x
    ansT[maxStopT[i]] = x

ans = [-1 if min(s, t) == float('inf') else min(s, t) for s, t in zip(ansS, ansT)]
print(*ans, sep='\n')

Submission status: Wrong Answer

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 - 1
        #  xの最小値を求める
        s = INF
        while L >= 0:
            s = min(s, data[L])
            L = (L - 1) >> 1
        return s

    for s, t, x in STX:
        XS = max(-1, s - x)
        XT = max(-1, t - x)
        XS_index = bisect_left(D, XS)
        XT_index = bisect_left(D, XT)
        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: Wrong Answer

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 - 1
        #  xの最小値を求める
        s = INF
        while L >= 0:
            s = min(s, data[L])
            L = (L - 1) >> 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)
        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: Wrong Answer

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 - 1
        #  xの最小値を求める
        s = INF
        while L >= 0:
            s = min(s, data[L])
            L = (L - 1) >> 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: Wrong Answer

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 = (L-1)>>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()