Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.remove(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():

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

    T = []
    X = [[0, 0, 0] for _ in range(n)]
    for i in range(n):
        s, t, x = map(int, input().split())
        T.append(s-x)
        T.append(t-x)
        X[i][0], X[i][1], X[i][2] = x, s-x, t-x

    X.sort(reverse=True)

    D = [0]*q
    for i in range(q):
        d = int(input())
        T.append(d)
        D[i] = d

    toid = {}
    T = list(set(T))
    T.sort()
    for i, t in enumerate(T):
        toid[t] = i

    m = len(T)
    # identity element
    ide_ele = 10**18

    A = [ide_ele]*m

    # num: n以上の最小の2のべき乗
    num = 2**(m-1).bit_length()
    seg = [ide_ele]*2*num
    lazy = [None]*2*num

    def segfunc(x, y):
        if x >= y:
            return y
        else:
            return x

    def init(init_val):
        # set_val
        for i in range(m):
            seg[i+num-1] = init_val[i]
        # built
        for i in range(num-2, -1, -1):
            seg[i] = segfunc(seg[2*i+1], seg[2*i+2])

    # 伝搬する区間のids(1-indexed)を全て列挙する
    # 降順で列挙される
    def gindex(p, q):
        p += num
        q += num
        # 1-indexedにしたとき、2*k > kの区間は更新する区間に含まれる
        # (p & -p)で最後の1bitを求めて、それで割ってからシフト
        pm = (p // (p & -p)) >> 1
        qm = (q // (q & -q)) >> 1
        while p < q:
            if q <= qm:
                yield q
            if p <= pm:
                yield p
            p = p >> 1
            q = q >> 1
        while p:
            yield p
            p = p >> 1

    # 1-indexedで降順のidを渡す
    def propagate(*ids):
        for i in reversed(ids):
            v = lazy[i-1]
            if v is None:
                continue
            lazy[2*i-1] = seg[2*i-1] = lazy[2*i] = seg[2*i] = v
            lazy[i-1] = None

    def update(p, q, x):
        *ids, = gindex(p, q)
        # 1. topdwon
        propagate(*ids)

        # 2. update
        p += num - 1
        q += num - 2
        while q-p > 1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                lazy[p] = seg[p] = x
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                lazy[q] = seg[q] = x
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            lazy[p] = seg[p] = x
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                lazy[p] = seg[p] = x
            else:
                lazy[p] = seg[p] = x
                lazy[q] = seg[q] = x

        # 3. bottom up
        for i in ids:
            seg[i-1] = segfunc(seg[2*i-1], seg[2*i])

    def query(p, q):
        if q <= p:
            return ide_ele

        # 1. topdwon
        #*ids, = gindex(p, q)
        propagate(*gindex(p, q))

        # 2. 区間[p, q)で取得
        p += num - 1
        q += num - 2
        res = ide_ele
        while q-p>1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[p])
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[q])
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            res = segfunc(res, seg[p])
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                res = segfunc(res, seg[p])
            else:
                res = segfunc(segfunc(res, seg[p]), seg[q])
        #print(p, q)
        return res

    init(A)

    for x, s, t in X:
        s = toid[s]
        t = toid[t]
        update(s, t, x)

    for d in D:
        d = toid[d]
        ans = query(d, d+1)
        if ans == ide_ele:
            print(-1)
        else:
            print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():

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

    T = []
    X = [[0, 0, 0] for _ in range(n)]
    for i in range(n):
        s, t, x = map(int, input().split())
        T.append(s-x)
        T.append(t-x)
        X[i][0], X[i][1], X[i][2] = x, s-x, t-x

    X.sort(reverse=True)

    D = [0]*q
    for i in range(q):
        d = int(input())
        T.append(d)
        D[i] = d

    toid = {}
    T = list(set(T))
    T.sort()
    import bisect
    for t in T:
        id = bisect.bisect_left(T, t)
        toid[t] = id

    m = len(T)
    # identity element
    ide_ele = 10**18

    A = [ide_ele]*m

    # num: n以上の最小の2のべき乗
    num = 2**(m-1).bit_length()
    seg = [ide_ele]*2*num
    lazy = [None]*2*num

    def segfunc(x, y):
        if x >= y:
            return y
        else:
            return x

    def init(init_val):
        # set_val
        for i in range(m):
            seg[i+num-1] = init_val[i]
        # built
        for i in range(num-2, -1, -1):
            seg[i] = segfunc(seg[2*i+1], seg[2*i+2])

    # 伝搬する区間のids(1-indexed)を全て列挙する
    # 降順で列挙される
    def gindex(p, q):
        p += num
        q += num
        # 1-indexedにしたとき、2*k > kの区間は更新する区間に含まれる
        # (p & -p)で最後の1bitを求めて、それで割ってからシフト
        pm = (p // (p & -p)) >> 1
        qm = (q // (q & -q)) >> 1
        while p < q:
            if q <= qm:
                yield q
            if p <= pm:
                yield p
            p = p >> 1
            q = q >> 1
        while p:
            yield p
            p = p >> 1

    # 1-indexedで降順のidを渡す
    def propagate(*ids):
        for i in reversed(ids):
            v = lazy[i-1]
            if v is None:
                continue
            lazy[2*i-1] = seg[2*i-1] = lazy[2*i] = seg[2*i] = v
            lazy[i-1] = None

    def update(p, q, x):
        *ids, = gindex(p, q)
        # 1. topdwon
        propagate(*ids)

        # 2. update
        p += num - 1
        q += num - 2
        while q-p > 1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                lazy[p] = seg[p] = x
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                lazy[q] = seg[q] = x
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            lazy[p] = seg[p] = x
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                lazy[p] = seg[p] = x
            else:
                lazy[p] = seg[p] = x
                lazy[q] = seg[q] = x

        # 3. bottom up
        for i in ids:
            seg[i-1] = segfunc(seg[2*i-1], seg[2*i])

    def query(p, q):
        if q <= p:
            return ide_ele

        # 1. topdwon
        #*ids, = gindex(p, q)
        propagate(*gindex(p, q))

        # 2. 区間[p, q)で取得
        p += num - 1
        q += num - 2
        res = ide_ele
        while q-p>1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[p])
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[q])
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            res = segfunc(res, seg[p])
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                res = segfunc(res, seg[p])
            else:
                res = segfunc(segfunc(res, seg[p]), seg[q])
        #print(p, q)
        return res

    init(A)

    for x, s, t in X:
        s = toid[s]
        t = toid[t]
        update(s, t, x)

    for d in D:
        d = toid[d]
        ans = query(d, d+1)
        if ans == ide_ele:
            print(-1)
        else:
            print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():

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

    def segfunc(x, y):
        if x >= y:
            return y
        else:
            return x

    def init(init_val):
        # set_val
        for i in range(n):
            seg[i+num-1] = init_val[i]
        # built
        for i in range(num-2, -1, -1):
            seg[i] = segfunc(seg[2*i+1], seg[2*i+2])

    # 伝搬する区間のids(1-indexed)を全て列挙する
    # 降順で列挙される
    def gindex(p, q):
        p += num
        q += num
        # 1-indexedにしたとき、2*k > kの区間は更新する区間に含まれる
        # (p & -p)で最後の1bitを求めて、それで割ってからシフト
        pm = (p // (p & -p)) >> 1
        qm = (q // (q & -q)) >> 1
        while p < q:
            if q <= qm:
                yield q
            if p <= pm:
                yield p
            p = p >> 1
            q = q >> 1
        while p:
            yield p
            p = p >> 1

    # 1-indexedで降順のidを渡す
    def propagate(*ids):
        for i in reversed(ids):
            v = lazy[i-1]
            if v is None:
                continue
            lazy[2*i-1] = seg[2*i-1] = lazy[2*i] = seg[2*i] = v
            lazy[i-1] = None

    def update(p, q, x):
        *ids, = gindex(p, q)
        # 1. topdwon
        propagate(*ids)

        # 2. update
        p += num - 1
        q += num - 2
        while q-p > 1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                lazy[p] = seg[p] = x
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                lazy[q] = seg[q] = x
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            lazy[p] = seg[p] = x
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                lazy[p] = seg[p] = x
            else:
                lazy[p] = seg[p] = x
                lazy[q] = seg[q] = x

        # 3. bottom up
        for i in ids:
            seg[i-1] = segfunc(seg[2*i-1], seg[2*i])

    def query(p, q):
        if q <= p:
            return ide_ele

        # 1. topdwon
        #*ids, = gindex(p, q)
        propagate(*gindex(p, q))

        # 2. 区間[p, q)で取得
        p += num - 1
        q += num - 2
        res = ide_ele
        while q-p>1:
            if p&1 == 0: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[p])
            if q&1 == 1: #左のnode: == 1, 右のnode: == 0
                res = segfunc(res, seg[q])
                q -= 1 # 隣に移動
            p = p//2 # 隣のnodeの親にアクセス
            q = (q-1)//2 #親のnodeにアクセス
        if p == q:
            res = segfunc(res, seg[p])
        else:
            if (p-1)//2 == (q-1)//2:
                p = (p-1)//2
                res = segfunc(res, seg[p])
            else:
                res = segfunc(segfunc(res, seg[p]), seg[q])
        #print(p, q)
        return res

    T = []
    X = [[0, 0, 0] for _ in range(n)]
    for i in range(n):
        s, t, x = map(int, input().split())
        T.append(s-x)
        T.append(t-x)
        X[i][0], X[i][1], X[i][2] = x, s-x, t-x

    X.sort(reverse=True)

    D = [0]*q
    for i in range(q):
        d = int(input())
        T.append(d)
        D[i] = d

    toid = {}
    T = list(set(T))
    T.sort()
    import bisect
    for t in T:
        id = bisect.bisect_left(T, t)
        toid[t] = id

    m = len(T)
    # identity element
    ide_ele = 10**18

    A = [ide_ele]*m

    # num: n以上の最小の2のべき乗
    num = 2**(m-1).bit_length()
    seg = [ide_ele]*2*num
    lazy = [None]*2*num

    init(A)

    for x, s, t in X:
        s = toid[s]
        t = toid[t]
        update(s, t, x)

    for d in D:
        d = toid[d]
        ans = query(d, d+1)
        if ans == ide_ele:
            print(-1)
        else:
            print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():

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

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

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

if __name__ == '__main__':
    main()