Problem p03033 (python)

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

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

    stx = []
    tmp = set([])

    for _ in range(n):
        s,t,x = map(int, input().split())
        stx.append((s,t,x))
        tmp.add(x-t)
        tmp.add(x-s)

    d = {}
    for i in range(q):
        di = -int(input())
        tmp.add(di)
        d[di] = i

    stx.sort(key = lambda x:x[2])

    v2i = {}

    for i, v in enumerate(sorted(tmp)):
        v2i[v] = i

    l = [1] * (len(tmp) + 1)

    for di in d:
        l[v2i[di]] = di
            

    #####単位元######
    ide_ele_min = 1

    #num_min:n以上の最小の2のべき乗
    N = len(l)

    num_min =2**(N-1).bit_length()
    seg_min=[ide_ele_min]*2*num_min

    def init_min(init_min_val):
        #set_val
        for i in range(N):
            seg_min[i+num_min-1]=init_min_val[i]    
        #built
        for i in range(num_min-2,-1,-1) :
            seg_min[i]=min(seg_min[2*i+1],seg_min[2*i+2]) 
    
    def update_min(k,x):
        k += num_min-1
        seg_min[k] = x
        while k:
            k = (k-1)//2
            seg_min[k] = min(seg_min[k*2+1],seg_min[k*2+2])
        
    def query_min(p,q):
        if q<=p:
            return ide_ele_min
        p += num_min-1
        q += num_min-2
        res=ide_ele_min
        while q-p>1:
            if p&1 == 0:
                res = min(res,seg_min[p])
            if q&1 == 1:
                res = min(res,seg_min[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = min(res,seg_min[p])
        else:
            res = min(min(res,seg_min[p]),seg_min[q])
        return res



    init_min(l)
    ans = [-1] * q

    for s, t, x in stx:
        l = v2i[x-t] + 1
        r = v2i[x-s] + 1
        v = query_min(l, r)
        while v != 1:
            i_d = d[v]
            i_sg = v2i[v]
            ans[i_d] = x
            update_min(i_sg, 1)
            v = query_min(l, r)

    for ansi in ans:
        print(ansi)
            
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    from operator import itemgetter
    input = sys.stdin.readline

    class SegTree():
        def __init__(self, A):
            n_ = len(A)
            self.n = 1
            while self.n < n_:
                self.n *= 2
            self.tree = [1] * (2 * self.n - 1)
            for k, a in enumerate(A):
                self.update(k, a)
        def update(self, k, a):
            k += self.n - 1
            self.tree[k] = a
            while k > 0:
                k = (k - 1) // 2
                self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
        def query(self, l, r):
            l += self.n - 1
            r += self.n - 2
            res = 1
            while r-l>1:
                if l&1 == 0:
                    res = min(res,self.tree[l])
                if r&1 == 1:
                    res = min(res,self.tree[r])
                    r -= 1
                l = l//2
                r = (r-1)//2
            if l == r:
                res = min(res,self.tree[l])
            else:
                res = min(min(res,self.tree[l]),self.tree[r])
            return res

    n, q = map(int, input().split())
    X = []
    stx = []
    tmp = set([])

    for _ in range(n):
        s,t,x = map(int, input().split())
        stx.append((s,t,x))
        X.append(x, _)
        tmp.add(x-t)
        tmp.add(x-s)

    d = {}
    for i in range(q):
        di = -int(input())
        tmp.add(di)
        d[di] = i

    X.sort(key = itemgetter(0))

    v2i = {}

    for i, v in enumerate(sorted(tmp)):
        v2i[v] = i

    l = [1] * (len(tmp) + 1)

    for di in d:
        l[v2i[di]] = di
            
    sgt = SegTree(l)
    ans = {}

    sgt_update = sgt.update
    sgt_query = sgt.query
        
    for x, i in X:
        s, t, x = stx[i]
        l = v2i[x-t] + 1
        r = v2i[x-s] + 1
        v = sgt_query(l, r)
        while v != 1:
            i_d = d[v]
            i_sg = v2i[v]
            ans[i_d] = x
            sgt_update(i_sg, 1)
            v = sgt_query(l, r)

    for i in range(q):
        if i in ans:
            print(ans[i])
        else:
            print(-1)
            
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline

    INF = 1

    class SegTree():
        def __init__(self, A):
            n_ = len(A)
            self.n = 1
            while self.n < n_:
                self.n *= 2
            self.tree = [INF for _ in range(2 * self.n - 1)]
            for k, a in enumerate(A):
                self.update(k, a)
        def update(self, k, a):
            k += self.n - 1
            self.tree[k] = a
            while k > 0:
                k = (k - 1) // 2
                self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
        def query(self, l, r):
            l += self.n - 1
            r += self.n - 2
            res = INF
            while r-l>1:
                if l&1 == 0:
                    res = min(res,self.tree[l])
                if r&1 == 1:
                    res = min(res,self.tree[r])
                    r -= 1
                l = l//2
                r = (r-1)//2
            if l == r:
                res = min(res,self.tree[l])
            else:
                res = min(min(res,self.tree[l]),self.tree[r])
            return res

    n, q = map(int, input().split())
    stx = []
    tmp = set([])

    for _ in range(n):
        s,t,x = map(int, input().split())
        stx.append([s,t,x])
        tmp.add(t-x)
        tmp.add(s-x)

    
    for i in range(q):
        tmp.add(-int(input()))

    stx.sort(key = lambda x:x[2])

    tmp = list(tmp)
    tmp.sort()

    l = []
    d2i = {}
    memo = - 10 ** 10
    i_sg = -1

    for val, cat, index in tmp:
        if val > memo:
            i_sg += 1
            l.append(1)
        if cat == "B":
            l[i_sg]=val
            d2i[val] = (i_sg, index)
        elif cat == "A":
            stx[index][0] = i_sg
        else:
            stx[index][1] = i_sg

        memo = val

            
    # sgt = SegTree(l + [1])
    # ans = [-1] * q

    # sgt_update = sgt.update
    # sgt_query = sgt.query
        
    # for s, t, x in stx:
    #     v = sgt_query(s+1, t+1)
    #     while v < 1:
    #         i_sg, i_d = d2i[v]
    #         ans[i_d] = x
    #         sgt_update(i_sg, 1)
    #         v = sgt_query(s+1, t+1)

    # for ansi in ans:
    #     print(ansi)
            

main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

INF = 1

class SegTree():
    def __init__(self, A):
        n_ = len(A)
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.tree = [INF for _ in range(2 * self.n - 1)]
        for k, a in enumerate(A):
            self.update(k, a)
    def update(self, k, a):
        k += self.n - 1
        self.tree[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
    def query(self, l, r):
        l += self.n - 1
        r += self.n - 2
        res = INF
        while r-l>1:
            if l&1 == 0:
                res = min(res,self.tree[l])
            if r&1 == 1:
                res = min(res,self.tree[r])
                r -= 1
            l = l//2
            r = (r-1)//2
        if l == r:
            res = min(res,self.tree[l])
        else:
            res = min(min(res,self.tree[l]),self.tree[r])
        return res

n, q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
d = [-int(input()) for _ in range(q)]
stx.sort(key = lambda x:x[2])


tmp = []

for i, di in enumerate(d):
    tmp.append([di, "B", i])

for i, (s, t, x) in enumerate(stx):
    tmp.append([x-s, "C", i])
    tmp.append([x-t, "A", i])    

tmp.sort()
l = [1] * (n+q)
d2i = {}
memo = - 10 ** 10
i_sg = -1

for val, cat, index in tmp:
    if val > memo:
        i_sg += 1
    if cat == "B":
        l[i_sg]=val
        d2i[val] = (i_sg, index)
    elif cat == "A":
        stx[index][0] = i_sg
    else:
        stx[index][1] = i_sg

    memo = val

        
sgt = SegTree(l + [1])
ans = [-1 for i in range(q)]
# print(sgt.tree)
# print(d2i)
    
for s, t, x in stx:
    # print(s, t, x)
    v = sgt.query(s+1, t+1)
    while v < 1:
        # print(v)
        i_sg, i_d = d2i[v]
        ans[i_d] = x
        sgt.update(i_sg, 1)
        v = sgt.query(s+1, t+1)

for ansi in ans:
    print(ansi)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

INF = 1

class SegTree():
    def __init__(self, A):
        n_ = len(A)
        self.n = 1
        while self.n < n_:
            self.n *= 2
        self.tree = [INF for _ in range(2 * self.n - 1)]
        for k, a in enumerate(A):
            self.update(k, a)
    def update(self, k, a):
        k += self.n - 1
        self.tree[k] = a
        while k > 0:
            k = (k - 1) // 2
            self.tree[k] = min(self.tree[k * 2 + 1], self.tree[k * 2 + 2])
    def query(self, l, r):
        l += self.n - 1
        r += self.n - 2
        res = INF
        while r-l>1:
            if l&1 == 0:
                res = min(res,self.tree[l])
            if r&1 == 1:
                res = min(res,self.tree[r])
                r -= 1
            l = l//2
            r = (r-1)//2
        if l == r:
            res = min(res,self.tree[l])
        else:
            res = min(min(res,self.tree[l]),self.tree[r])
        return res

n, q = map(int, input().split())
stx = [list(map(int, input().split())) for _ in range(n)]
d = [-int(input()) for _ in range(q)]
stx.sort(key = lambda x:x[2])


tmp = []

for i, di in enumerate(d):
    tmp.append([di, "B", i])

for i, (s, t, x) in enumerate(stx):
    tmp.append([x-s, "C", i])
    tmp.append([x-t, "A", i])    

tmp.sort()
l = [1] * (n+q)
d2i = {}
memo = set([])
i_sg = -1

for val, cat, index in tmp:
    if val not in memo:
        i_sg += 1
    if cat == "B":
        l[i_sg]=val
        d2i[val] = (i_sg, index)
    elif cat == "A":
        stx[index][0] = i_sg
    else:
        stx[index][1] = i_sg

    memo.add(val)

        
sgt = SegTree(l + [1])
ans = [-1 for i in range(q)]
# print(sgt.tree)
# print(d2i)
    
for s, t, x in stx:
    # print(s, t, x)
    v = sgt.query(s+1, t+1)
    while v < 1:
        # print(v)
        i_sg, i_d = d2i[v]
        ans[i_d] = x
        sgt.update(i_sg, 1)
        v = sgt.query(s+1, t+1)

for ansi in ans:
    print(ansi)