Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = np.full(Q, 10**9, dtype=np.int)
    mask = np.full(Q, -1, dtype=np.int)
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        ans[left:right] = np.minimum(ans[left:right], pos)
        mask[left:right] = pos

    ans = np.minimum(ans, mask)

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if ans[left] == -1:
                ans[left] = pos

            left += 1

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure = sorted(closure, key=lambda x: x[0])

    D = []
    index = defaultdict(lambda: 0)
    for i in range(Q):
        d = int(input())
        D.append(d)
        index[d] = i

    D.sort()

    ans = [0] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        for blocked in D[left:right]:
            ans[index[blocked]] = pos

        D = D[0:left] + D[right:]

    for blocked in D:
        ans[index[blocked]] = -1

    return ans



if __name__ == '__main__':
    print(*main(), sep='\n')


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        heappush(closure, (pos, start, end))

    D = [0] * Q
    index = dict()
    for i in range(Q):
        d = int(input())
        D[i] = d

    ans = []
    while len(closure) > 0:
        pos, start, end = heappop(closure)

        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        for blocked in D[left:right]:
            heappush(ans, (blocked, pos))

        D = D[0:left] + D[right:]

    for blocked in D:
        heappush(ans, (blocked, -1))

    while len(ans) > 0:
        _, pos = heappop(ans)
        print(pos)


if __name__ == '__main__':
    main()


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure = sorted(closure, key=lambda x: x[0])

    D = []
    index = defaultdict(lambda: 0)
    for i in range(Q):
        d = int(input())
        D.append(d)
        index[d] = i

    D.sort()

    ans = [0] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        for blocked in D[left:right]:
            ans[index[blocked]] = pos

        D = D[0:left] + D[right:]

    for blocked in D:
        ans[index[blocked]] = -1

    return ans



if __name__ == '__main__':
    print(*main(), sep='\n')


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure = sorted(closure, key=lambda x: x[0])

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

    D.sort()

    d = defaultdict(lambda: 0)
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        for blocked in D[left:right]:
            d[blocked] = pos

        D = D[0:left] + D[right:]

    for blocked in D:
        d[blocked] = -1

    for key in sorted(d.keys()):
        print(d[key])


if __name__ == '__main__':
    main()


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import bisect_left
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure = sorted(closure, key=lambda x: x[0])

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

    D.sort()

    d = defaultdict(lambda: 0)
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        for blocked in D[left:right]:
            d[blocked] = pos

        D = D[0:left] + D[right:]

    for blocked in D:
        d[blocked] = -1

    for key in sorted(d.keys()):
        print(d[key])


if __name__ == '__main__':
    main()


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    X = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        X.append((pos, start, end))

    X = sorted(X, key=lambda x: x[0])
    positions = np.zeros(N+1, dtype=np.int)
    starts = np.zeros(N+1, dtype=np.int)
    ends = np.zeros(N+1, dtype=np.int)
    for i, (pos, start, end) in enumerate(X):
        positions[i] = pos
        starts[i] = start
        ends[i] = end

    positions[N] = -1
    starts[N] = -1
    ends[N] = 10**9

    for _ in range(Q):
        d = int(input())

        before = (positions + d) >= starts
        after = (positions + d) < ends
        check = before * after

        print(positions[check][0])


if __name__ == '__main__':
    main()


Fixed submission:
# ref: https://atcoder.jp/contests/abc128/submissions/5654623

import sys
from bisect import bisect_left

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    closure = []
    for _ in range(N):
        start, end, pos = map(int, input().split())
        closure.append((pos, start, end))

    closure.sort()

    D = [int(input()) for _ in range(Q)]

    ans = [-1] * Q
    skip = [-1] * Q
    for pos, start, end in closure:
        left = bisect_left(D, start - pos)
        right = bisect_left(D, end - pos)

        while left < right:
            if skip[left] == -1:
                ans[left] = pos
                skip[left] = right
                left += 1
            else:
                left = skip[left]

    return ans


if __name__ == '__main__':
    print(*main(), sep='\n')