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

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

# 区間更新、区間最小
class LazySegMin:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [inf] * (2 * n)
        lazy = [None] * (2 * n)
        tree[n:n + len(aa)] = aa
        for u in range(n - 1, -1, -1):
            tree[u] = min(tree[u * 2], tree[u * 2 + 1])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def update(self, l, r, a, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        self._eval(u, tl, tr)
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] = a
            self._eval(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.update(l, r, a, u * 2, tl, tm)
        self.update(l, r, a, u * 2 + 1, tm, tr)
        self.tree[u] = min(self.tree[u * 2], self.tree[u * 2 + 1])

    def min(self, l, r, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return self.inf
        self._eval(u, tl, tr)
        if l <= tl and tr <= r: return self.tree[u]
        tm = (tl + tr) // 2
        return min(self.min(l, r, u * 2, tl, tm), self.min(l, r, u * 2 + 1, tm, tr))

    def _eval(self, u, l, r):
        if self.lazy[u] == None: return
        self.tree[u] = self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2] = self.lazy[u]
            self.lazy[u * 2 + 1] = self.lazy[u]
        self.lazy[u] = None

from heapq import *

inf=10**9
h,w=MI()

cost=[0]*w
invaid=0
hp=[(0,i) for i in range(w)]
heapify(hp)
st=LazySegMin(list(range(0,-w,-1)))
to0=lambda a,k:(a>>(k+1))<<(k+1)|(a&((1<<k)-1))


for i in range(h):
    l,r=MI()
    l-=1
    if r<w:
        mn=st.min(l,r+1)
        st.update(r,r+1,mn)
        invaid=to0(invaid,r)
        cost[r]=mn+r
        heappush(hp,(mn+r,r))
    st.update(l,r,inf)
    invaid|=((1<<r-l)-1)<<l
    while hp:
        c,j=hp[0]
        if invaid>>j&1 or cost[j]!=c:heappop(hp)
        else:
            print(c+i+1)
            break
    else:print(-1)
    # print(l,r,cost,bin(invaid),hp)


Fixed submission:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class BitSum:
    def __init__(self, n):
        self.n = n + 1
        self.table = [0] * self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

    # 数列を度数分布とみたときに、x番目がどのインデックスにあるかを返す
    def rank(self, x):
        idx = 0
        for lv in range((self.n - 1).bit_length() - 1, -1, -1):
            mid = idx + (1 << lv)
            if mid >= self.n: continue
            if self.table[mid] < x:
                x -= self.table[mid]
                idx += 1 << lv
        return idx

def solve():
    bit = BitSum(w + 1)
    for j in range(w): bit.add(j + 1, 1)
    cost = [0] * (w + 1)
    hp = [0] * w
    heapify(hp)
    rem = []

    for i in range(h):
        l, r = MI()
        r += 1
        # cost[r]の更新
        if r <= w:
            cnt = bit.sum(r)
            if cnt:
                j = bit.rank(cnt)
                if l <= j < r:
                    bit.add(r, 1)
                    cost[r] = cost[j] + r - j
                    heappush(hp, cost[r])
        # 区間[l,r)を無効にする
        cnt = bit.sum(l - 1)
        while 1:
            j = bit.rank(cnt + 1)
            if j >= r: break
            heappush(rem, cost[j])
            bit.add(j, -1)
        # ヒープからの削除
        while hp and rem and hp[0] == rem[0]:
            heappop(hp)
            heappop(rem)
        # 出力
        if hp: print(hp[0] + i + 1)
        else: print(-1)

h,w=MI()
solve()

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

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

# 区間更新、区間最小
class LazySegMin:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [inf] * (2 * n)
        lazy = [None] * (2 * n)
        tree[n:n + len(aa)] = aa
        for u in range(n - 1, -1, -1):
            tree[u] = min(tree[u * 2], tree[u * 2 + 1])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def update(self, l, r, a, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        self._eval(u, tl, tr)
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] = a
            self._eval(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.update(l, r, a, u * 2, tl, tm)
        self.update(l, r, a, u * 2 + 1, tm, tr)
        self.tree[u] = min(self.tree[u * 2], self.tree[u * 2 + 1])

    def min(self, l, r, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return self.inf
        self._eval(u, tl, tr)
        if l <= tl and tr <= r: return self.tree[u]
        tm = (tl + tr) // 2
        return min(self.min(l, r, u * 2, tl, tm), self.min(l, r, u * 2 + 1, tm, tr))

    def _eval(self, u, l, r):
        if self.lazy[u] == None: return
        self.tree[u] = self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2] = self.lazy[u]
            self.lazy[u * 2 + 1] = self.lazy[u]
        self.lazy[u] = None

from heapq import *

inf=10**9
h,w=MI()

cost=[0]*w
invaid=0
hp=[(0,i) for i in range(w)]
heapify(hp)
st=LazySegMin(list(range(0,-w,-1)))
to0=lambda a,k:(a>>(k+1))<<(k+1)|((1<<k)-1)


for i in range(h):
    l,r=MI()
    l-=1
    if r<w:
        mn=st.min(l,r+1)
        st.update(r,r+1,mn)
        invaid=to0(invaid,r)
        cost[r]=mn+r
        heappush(hp,(mn+r,r))
    st.update(l,r,inf)
    invaid|=((1<<r-l)-1)<<l
    while hp:
        c,j=hp[0]
        if invaid>>j&1 or cost[j]!=c:heappop(hp)
        else:
            print(c+i+1)
            break
    else:print(-1)
    # print(l,r,cost,bin(invaid),hp)


Fixed submission:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class BitSum:
    def __init__(self, n):
        self.n = n + 1
        self.table = [0] * self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

    # 数列を度数分布とみたときに、x番目がどのインデックスにあるかを返す
    def rank(self, x):
        idx = 0
        for lv in range((self.n - 1).bit_length() - 1, -1, -1):
            mid = idx + (1 << lv)
            if mid >= self.n: continue
            if self.table[mid] < x:
                x -= self.table[mid]
                idx += 1 << lv
        return idx

def solve():
    bit = BitSum(w + 1)
    for j in range(w): bit.add(j + 1, 1)
    cost = [0] * (w + 1)
    hp = [0] * w
    heapify(hp)
    rem = []

    for i in range(h):
        l, r = MI()
        r += 1
        # cost[r]の更新
        if r <= w:
            cnt = bit.sum(r)
            if cnt:
                j = bit.rank(cnt)
                if l <= j < r:
                    bit.add(r, 1)
                    cost[r] = cost[j] + r - j
                    heappush(hp, cost[r])
        # 区間[l,r)を無効にする
        cnt = bit.sum(l - 1)
        while 1:
            j = bit.rank(cnt + 1)
            if j >= r: break
            heappush(rem, cost[j])
            bit.add(j, -1)
        # ヒープからの削除
        while hp and rem and hp[0] == rem[0]:
            heappop(hp)
            heappop(rem)
        # 出力
        if hp: print(hp[0] + i + 1)
        else: print(-1)

h,w=MI()
solve()

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

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

# 区間更新、区間最大
class LazySegMax:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [-inf] * (2 * n)
        lazy = [None] * (2 * n)
        tree[n:n + len(aa)] = aa
        for u in range(n - 1, 0, -1): tree[u] = max(tree[u * 2], tree[u * 2 + 1])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def update(self, l, r, a, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        self._eval(u)
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] = a
            self._eval(u)
            return
        tm = (tl + tr) // 2
        self.update(l, r, a, u * 2, tl, tm)
        self.update(l, r, a, u * 2 + 1, tm, tr)
        self.tree[u] = max(self.tree[u * 2], self.tree[u * 2 + 1])

    def max(self, l, r, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return -self.inf
        self._eval(u)
        if l <= tl and tr <= r: return self.tree[u]
        tm = (tl + tr) // 2
        return max(self.max(l, r, u * 2, tl, tm), self.max(l, r, u * 2 + 1, tm, tr))

    def _eval(self, u):
        if self.lazy[u]==None:return
        self.tree[u] = self.lazy[u]
        if u < self.n:
            self.lazy[u * 2] = self.lazy[u]
            self.lazy[u * 2 + 1] = self.lazy[u]
        self.lazy[u]=None

# 区間更新、区間最小
class LazySegMin:
    def __init__(self, aa):
        inf = 10 ** 16
        n = 1 << (len(aa) - 1).bit_length()
        tree = [inf] * (2 * n)
        lazy = [None] * (2 * n)
        tree[n:n + len(aa)] = aa
        for u in range(n - 1, -1, -1):
            tree[u] = min(tree[u * 2], tree[u * 2 + 1])
        self.tree = tree
        self.lazy = lazy
        self.n = n
        self.inf = inf

    def update(self, l, r, a, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        self._eval(u, tl, tr)
        if r <= tl or tr <= l: return
        if l <= tl and tr <= r:
            self.lazy[u] = a
            self._eval(u, tl, tr)
            return
        tm = (tl + tr) // 2
        self.update(l, r, a, u * 2, tl, tm)
        self.update(l, r, a, u * 2 + 1, tm, tr)
        self.tree[u] = min(self.tree[u * 2], self.tree[u * 2 + 1])

    def min(self, l, r, u=1, tl=0, tr=-1):
        if tr == -1: tr = self.n
        if r <= tl or tr <= l: return self.inf
        self._eval(u, tl, tr)
        if l <= tl and tr <= r: return self.tree[u]
        tm = (tl + tr) // 2
        return min(self.min(l, r, u * 2, tl, tm), self.min(l, r, u * 2 + 1, tm, tr))

    def _eval(self, u, l, r):
        if self.lazy[u] == None: return
        self.tree[u] = self.lazy[u]
        if l + 1 < r:
            self.lazy[u * 2] = self.lazy[u]
            self.lazy[u * 2 + 1] = self.lazy[u]
        self.lazy[u] = None

inf=10**9
h,w=MI()
# first position
fp=LazySegMax(list(range(w)))
# cost
cs=LazySegMin([0]*w)

for i in range(h):
    l,r=MI()
    l-=1
    if r<w:
        mxp=fp.max(l,r+1)
        fp.update(r,r+1,mxp)
        cs.update(r,r+1,r-mxp)
    fp.update(l, r, -1)
    cs.update(l,r,inf)
    cur=cs.min(0,w)
    if cur==inf:print(-1)
    else:print(cur+i+1)


Fixed submission:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class BitSum:
    def __init__(self, n):
        self.n = n + 1
        self.table = [0] * self.n

    def add(self, i, x):
        i += 1
        while i < self.n:
            self.table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self.table[i]
            i -= i & -i
        return res

    # 数列を度数分布とみたときに、x番目がどのインデックスにあるかを返す
    def rank(self, x):
        idx = 0
        for lv in range((self.n - 1).bit_length() - 1, -1, -1):
            mid = idx + (1 << lv)
            if mid >= self.n: continue
            if self.table[mid] < x:
                x -= self.table[mid]
                idx += 1 << lv
        return idx

def solve():
    bit = BitSum(w + 1)
    for j in range(w): bit.add(j + 1, 1)
    cost = [0] * (w + 1)
    hp = [0] * w
    heapify(hp)
    rem = []

    for i in range(h):
        l, r = MI()
        r += 1
        # cost[r]の更新
        if r <= w:
            cnt = bit.sum(r)
            if cnt:
                j = bit.rank(cnt)
                if l <= j < r:
                    bit.add(r, 1)
                    cost[r] = cost[j] + r - j
                    heappush(hp, cost[r])
        # 区間[l,r)を無効にする
        cnt = bit.sum(l - 1)
        while 1:
            j = bit.rank(cnt + 1)
            if j >= r: break
            heappush(rem, cost[j])
            bit.add(j, -1)
        # ヒープからの削除
        while hp and rem and hp[0] == rem[0]:
            heappop(hp)
            heappop(rem)
        # 出力
        if hp: print(hp[0] + i + 1)
        else: print(-1)

h,w=MI()
solve()