Problem:


Score : 600 points

Problem Statement

There is a grid of squares with H+1 horizontal rows and W vertical columns.

You will start at one of the squares in the top row and repeat moving one square right or down. However, for each integer i from 1 through H, you cannot move down from the A_i-th, (A_i + 1)-th, \ldots, B_i-th squares from the left in the i-th row from the top.

For each integer k from 1 through H, find the minimum number of moves needed to reach one of the squares in the (k+1)-th row from the top. (The starting square can be chosen individually for each case.) If, starting from any square in the top row, none of the squares in the (k+1)-th row can be reached, print -1 instead.

Constraints

1 \leq H,W \leq 2\times 10^5

1 \leq A_i \leq B_i \leq W

Input

Input is given from Standard Input in the following format:

H W
A_1 B_1
A_2 B_2
:
A_H B_H

Output

Print H lines. The i-th line should contain the answer for the case k=i.

Sample Input 1

4 4
2 4
1 1
2 3
2 4

Sample Output 1

1
3
6
-1

Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For k=1, we need one move such as (1,1) → (2,1).

For k=2, we need three moves such as (1,1) → (2,1) → (2,2) → (3,2).

For k=3, we need six moves such as (1,1) → (2,1) → (2,2) → (3,2) → (3,3) → (3,4) → (4,4).

For k=4, it is impossible to reach any square in the fifth row from the top.



Sample input:
4 4
2 4
1 1
2 3
2 4


Sample output:
1
3
6
-1

Submission status: Time Limit Exceeded

Solution code:
class LazySegmentTree:

    __slots__ = ["n", "data", "lazy", "me", "oe", "fmm", "fmo", "foo"]

    def __init__(self, monoid_data, monoid_identity, operator_identity, func_monoid_monoid, func_monoid_operator, func_operator_operator):
        self.me = monoid_identity
        self.oe = operator_identity
        self.fmm = func_monoid_monoid
        self.fmo = func_monoid_operator
        self.foo = func_operator_operator

        self.n = len(monoid_data)
        self.data = monoid_data * 2
        for i in range(self.n-1, 0, -1):
            self.data[i] = self.fmm(self.data[2*i], self.data[2*i+1])
        self.lazy = [self.oe] * (self.n * 2)


    def replace(self, index, value):
        index += self.n

        # propagation
        for shift in range(index.bit_length()-1, 0, -1):
            i = index >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # update
        self.data[index] = value
        self.lazy[index] = self.oe

        # recalculation
        i = index
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe


    def effect(self, l, r, operator):
        l += self.n
        r += self.n
        l0 = l
        r0 = r - 1
        while l0 % 2 == 0:
            l0 //= 2
        while r0 % 2 == 1:
            r0 //= 2

        # propagation
        for shift in range(l0.bit_length()-1, 0, -1):
            i = l0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe
        for shift in range(r0.bit_length()-1, 0, -1):
            i = r0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # effect
        while l < r:
            if l % 2:
                self.lazy[l] = self.foo(self.lazy[l], operator)
                l += 1
            if r % 2:
                r -= 1
                self.lazy[r] = self.foo(self.lazy[r], operator)
            l //= 2
            r //= 2

        # recalculation
        i = l0
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe
        i = r0
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe
            
        
    def folded(self, l, r):
        l += self.n
        r += self.n
        l0 = l
        r0 = r - 1
        while l0 % 2 == 0:
            l0 //= 2
        while r0 % 2 == 1:
            r0 //= 2

        # propagation
        for shift in range(l0.bit_length()-1, 0, -1):
            i = l0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe
        for shift in range(r0.bit_length()-1, 0, -1):
            i = r0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # fold
        left_folded = self.me
        right_folded = self.me
        while l < r:
            if l % 2:
                left_folded = self.fmm(left_folded, self.fmo(self.data[l], self.lazy[l]))
                l += 1
            if r % 2:
                r -= 1
                right_folded = self.fmm(self.fmo(self.data[r], self.lazy[r]), right_folded)
            l //= 2
            r //= 2
        return self.fmm(left_folded, right_folded)

def fmo(m1, o1):
    if o1 == -1:
        return m1
    return o1
def foo(o1, o2):
    if o2 == -1:
        return o1
    return o2
      

import sys
input = sys.stdin.readline
def main():
  h, w = map(int, input().split())
  lst_max = LazySegmentTree(list(range(w+1)), -float("inf"), -1, max, fmo, foo)
  lst_min = LazySegmentTree([0]*(w+1), float("inf"), -1, min, fmo, foo)
  for i in range(h):
      a, b = map(int, input().split())
      a -= 1
      l_max = lst_max.folded(a, b)
      lst_max.effect(a, b, -float("inf"))
      x = lst_max.folded(b, b+1)
      lst_max.replace(b, max(l_max, x))
      lst_min.effect(a, b, float("inf"))
      y = lst_min.folded(b, b+1)
      lst_min.replace(b, min(b, y))
      ans = lst_min.folded(0, w)
      if ans == float("inf"):
          print(-1)
      else:
          ans += i+1
          print(ans)
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
class LazySegmentTree:

    __slots__ = ["n", "data", "lazy", "me", "oe", "fmm", "fmo", "foo"]

    def __init__(self, monoid_data, monoid_identity, operator_identity, func_monoid_monoid, func_monoid_operator, func_operator_operator):
        self.me = monoid_identity
        self.oe = operator_identity
        self.fmm = func_monoid_monoid
        self.fmo = func_monoid_operator
        self.foo = func_operator_operator

        self.n = len(monoid_data)
        self.data = monoid_data * 2
        for i in range(self.n-1, 0, -1):
            self.data[i] = self.fmm(self.data[2*i], self.data[2*i+1])
        self.lazy = [self.oe] * (self.n * 2)


    def replace(self, index, value):
        index += self.n

        # propagation
        for shift in range(index.bit_length()-1, 0, -1):
            i = index >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # update
        self.data[index] = value
        self.lazy[index] = self.oe

        # recalculation
        i = index
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe


    def effect(self, l, r, operator):
        l += self.n
        r += self.n
        l0 = l
        r0 = r - 1
        while l0 % 2 == 0:
            l0 //= 2
        while r0 % 2 == 1:
            r0 //= 2

        # propagation
        for shift in range(l0.bit_length()-1, 0, -1):
            i = l0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe
        for shift in range(r0.bit_length()-1, 0, -1):
            i = r0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # effect
        while l < r:
            if l % 2:
                self.lazy[l] = self.foo(self.lazy[l], operator)
                l += 1
            if r % 2:
                r -= 1
                self.lazy[r] = self.foo(self.lazy[r], operator)
            l //= 2
            r //= 2

        # recalculation
        i = l0
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe
        i = r0
        while i > 1:
            i //= 2
            self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
            self.lazy[i] = self.oe
            
        
    def folded(self, l, r):
        l += self.n
        r += self.n
        l0 = l
        r0 = r - 1
        while l0 % 2 == 0:
            l0 //= 2
        while r0 % 2 == 1:
            r0 //= 2

        # propagation
        for shift in range(l0.bit_length()-1, 0, -1):
            i = l0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe
        for shift in range(r0.bit_length()-1, 0, -1):
            i = r0 >> shift
            self.lazy[2*i]   = self.foo(self.lazy[2*i],   self.lazy[i])
            self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
            self.data[i] = self.fmo(self.data[i], self.lazy[i])
            self.lazy[i] = self.oe

        # fold
        left_folded = self.me
        right_folded = self.me
        while l < r:
            if l % 2:
                left_folded = self.fmm(left_folded, self.fmo(self.data[l], self.lazy[l]))
                l += 1
            if r % 2:
                r -= 1
                right_folded = self.fmm(self.fmo(self.data[r], self.lazy[r]), right_folded)
            l //= 2
            r //= 2
        return self.fmm(left_folded, right_folded)

import sys
input = sys.stdin.readline
h, w = map(int, input().split())
def fmo(m1, o1):
    if o1 == -1:
        return m1
    return o1
def foo(o1, o2):
    if o2 == -1:
        return o1
    return o2
lst_max = LazySegmentTree(list(range(w+1)), -float("inf"), -1, max, fmo, foo)
lst_min = LazySegmentTree([0]*(w+1), float("inf"), -1, min, fmo, foo)
for i in range(h):
    a, b = map(int, input().split())
    a -= 1
    l_max = lst_max.folded(a, b)
    lst_max.effect(a, b, -float("inf"))
    x = lst_max.folded(b, b+1)
    lst_max.replace(b, max(l_max, x))
    lst_min.effect(a, b, float("inf"))
    y = lst_min.folded(b, b+1)
    lst_min.replace(b, min(b, y))
    ans = lst_min.folded(0, w)
    if ans == float("inf"):
        print(-1)
    else:
        ans += i+1
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:UTF-8
import sys
from math import factorial

MOD = 10 ** 9 + 7
INF = 10000000000

# ------ 入力 ------#
H, W = list(map(int, input().split()))  # スペース区切り連続数字
x = H
aList = [list(map(int, input().split())) for _ in range(x)]  # スペース区切り連続数字(行列)
aMap = [[0] * W for _ in range(H + 1)]

for i in range(len(aList)):
    for j in range(aList[i][0]-1, aList[i][1]):
        aMap[i][j] = 1

# 動的計画法DP
Interm = [[INF] * W for _ in range(H + 1)]
for i in range(W):
    Interm[0][i] = 0

def dp(n, w):
    if n < 1:
        return 0
    if w < 0:
        return -1

    if Interm[n][w] == INF:
        # 計算値
        if aMap[n-1][w] == 1:
            if dp(n, w-1) == -1:
                Interm[n][w] = -1
            else:
                Interm[n][w] = dp(n, w-1) + 1
        else:
            if dp(n, w-1) == -1 and dp(n-1, w) == -1:
                Interm[n][w] = -1
            elif dp(n, w-1) == -1:
                Interm[n][w] = dp(n-1, w) + 1
            elif dp(n-1, w) == -1:
                Interm[n][w] = dp(n, w-1) + 1
            else:
                Interm[n][w] = min(dp(n, w-1), dp(n-1, w)) + 1

    return Interm[n][w]

def main():

    # ------ 出力 ------#
    for i in range(1, H+1):
        m = -1
        for j in range(W):
            t = dp(i, j)
            if t != -1:
                if m == -1:
                    m = t
                else:
                    m = min(m, t)
        print(m)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(H)]

m =  [[-1 for _ in range(W)] for _ in range(H+1)]
m[0] = [0 for _ in range(W)]

for i in range(1,H):
  for j in range(W):
    if (j < AB[i-1][0] -1 or AB[i-1][1]-1 < j) and m[i-1][j] != -1:
      m[i][j] = m[i-1][j] + 1
    elif j > 0 and m[i][j-1] != -1:
      m[i][j] = m[i][j-1] +1

for s in m[1:] :
  ss = set(s)
  if max(ss) == -1:
    print (-1)
  else:
    if -1 in ss:
      ss.remove(-1)
    print(min(ss))

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
k = h*w
s = [i for i in range(1,w+1)]
l = [0]*w
for i in range(h):
  a,b = map(int,input().split())
  for j in range(w):
    if s[j] < a or b < s[j]:
      l[j] += 1
    else:
      l[j] += b-s[j]+2
      s[j] = b+1
    if s[j] > w:
      l[j] = k
  p = min(l)
  if p==k:
    p = -1
  print(p)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
k = h*w
d = [k]+[0]*w
s = 1
for i in range(h):
  a,b = map(int,input().split())
  if a <= s:
    s = b + 1
  d[s-1] = k
  for j in range(s,w+1):
    if j < a or j > b:
      d[j] = min(d[j]+1,d[j-1]+1)
    else:
      d[j] = d[j-1]+1
  if s == w+1:
    p = -1
  else:
    p = min(d[s:])
  print(p)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
k = h*w
d = [k]+[0]*w
s = 1
for i in range(h):
  a,b = map(int,input().split())
  if a <= s:
    s = b + 1
  for j in range(s,a):
    d[j] = min(d[j]+1,d[j-1]+1)
  for j in range(a,b+1):
    d[j] = d[j-1]+1
  for j in range(b+1,w+1):
    d[j] = min(d[j]+1,d[j-1]+1)
  p = min(d)
  if p == k:
    p = -1
  print(p)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())

A=[]
B=[]
X=[]
for i in range(H):
    a,b=map(lambda x:int(x)-1,input().split())
    for j in range(a,b+1):
        X.append([i,j])
    
#dpの作成
dp=[[[float('inf')]*W for i in range(H+1)]for i in range(W)]
for i in range(W):
    for j in range(W):
        dp[i][0][j]=0

#ループ
for t in range(W):
    for s in range(t):
        dp[t][0][s]=float('inf')
    for i in range(1,H+1):
        for j in range(W):
            if j ==0:
                if [i-1,j] in X:
                    continue
                else:
                    dp[t][i][j]=dp[t][i-1][j]+1
            else:
                if [i-1,j] in X:
                    dp[t][i][j]=dp[t][i][j-1]+1
                else:
                    dp[t][i][j]=min(dp[t][i-1][j]+1,dp[t][i][j-1]+1)

#出力
for i in range(H):
    ans=float('inf')
    for t in range(W):
        for j in range(W):
            ans = min(ans,dp[t][i+1][j])
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)