Problem p03033 (python)

Fixed submission:
import sys, collections, heapq
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, Q = map(int, line_input())
    event = []
    for i in range(N):
        s, t, x = map(int, line_input())
        event.append((s-x, 1, x))
        event.append((t-x,-1, x))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, d))
    event.sort()
    candidate = []
    heapq.heapify(candidate)
    stop = collections.defaultdict(int)
    ans = ""
    for time, parameter, co in event:
        if parameter == 1:
            heapq.heappush(candidate, co)
            stop[co] += 1
        elif parameter == -1:
            stop[co] -= 1
        else:
            while candidate:
                x = heapq.heappop(candidate)
                if stop[x] > 0:
                    heapq.heappush(candidate, x)
                    ans += str(x) + "\n"
                    break
            else: ans += "-1\n"
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, collections, heapq
def single_input(F): return F.readline().strip("\n")
def line_input(F): return F.readline().strip("\n").split()

def solve():
    F = sys.stdin
    N, Q = map(int, line_input())
    event = []
    for i in range(N):
        s, t, x = map(int, line_input())
        event.append((s-x, 1, x))
        event.append((t-x,-1, x))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, d))
    event.sort()
    candidate = []
    heapq.heapify(candidate)
    stop = collections.defaultdict(int)
    ans = ""
    for time, parameter, co in event:
        if parameter == 1:
            heapq.heappush(candidate, co)
            stop[co] += 1
        elif parameter == -1:
            stop[co] -= 1
        else:
            while candidate:
                x = heapq.heappop(candidate)
                if stop[x] > 0:
                    heapq.heappush(candidate, x)
                    ans += str(x) + "\n"
                    break
            else: ans += "-1\n"
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, Q = map(int, line_input())
    event = []
    for i in range(N):
        s, t, x = map(int, line_input())
        event.append((max(s-x, 0), 1, x))
        event.append((max(t-x, 0), -1, x))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, d))
    event.sort()
    stop = []
    ans = ""
    for time, parameter, co in event:
        if parameter == 1: stop.append(co)
        elif parameter == -1: stop.remove(co)
        else:
            if stop:
                stop.sort()
                ans += str(stop[0]) + "\n"
            else: ans += "-1\n"
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, Q = map(int, line_input())
    event = []
    for i in range(N):
        s, t, x = map(int, line_input())
        event.append((max(s-x, 0), 1, x, i))
        event.append((max(t-x, 0), -1, x, i))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, 0, 0))
    event.sort()
    stop = []
    heapq.heapify(stop)
    ans = ""
    for time, parameter, co, ci in event:
        if parameter == 1: heapq.heappush(stop, (co, i))
        elif parameter == -1: stop.remove((co, i))
        else:
            if stop: ans += str(stop[0][0]) + "\n"
            else: ans += "-1\n"
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, Q = map(int, line_input())
    event = []
    for i in range(N):
        s, t, x = map(int, line_input())
        event.append((max(s-x, 0), 1, x))
        event.append((max(t-x, 0), -1, x))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, 0))
    event.sort()
    stop = []
    heapq.heapify(stop)
    ans = ""
    for time, parameter, co in event:
        if parameter == 1: heapq.heappush(stop, co)
        elif parameter == -1: stop.remove(co)
        else:
            if stop: ans += str(stop[0]) + "\n"
            else: ans += "-1\n"
    print(ans)

    return 0

if __name__ == "__main__":
    solve()