Problem: Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.  Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region?  Process all the queries.

Submission attempts (in order):

def main():
    import sys
    import numpy as np
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x=="1", input())) for _ in range(H)]
    M = np.zeros((H + 1, W + 1), dtype=np.int32)
    L = np.zeros((H + 1, W + 1), dtype=np.int32)
    R = np.zeros((H + 1, W + 1), dtype=np.int32)
    U = np.zeros((H + 1, W + 1), dtype=np.int32)
    D = np.zeros((H + 1, W + 1), dtype=np.int32)

    for x, s in enumerate(S):
        for y, c in enumerate(s):
            m = l = r = u = d = 0
            if c:
                if x >= 1:
                    m += S[x-1][y]
                    u -= S[x-1][y]
                if x < H-1:
                    m += S[x+1][y]
                    d -= S[x+1][y]
                if y >= 1:
                    m += s[y-1]
                    l -= s[y-1]
                if y < W-1:
                    m += s[y+1]
                    r -= s[y+1]
                m = 2 - m
                M[x, y] = m
                L[x, y] = l
                R[x, y] = r
                U[x, y] = u
                D[x, y] = d

    M = M.cumsum(axis=0).cumsum(axis=1)
    L = L.cumsum(axis=0)
    R = R.cumsum(axis=0)
    U = U.cumsum(axis=1)
    D = D.cumsum(axis=1)
    M[:, -1] = 0
    M[-1, :] = 0
    L[-1, :] = 0
    R[-1, :] = 0
    U[:, -1] = 0
    D[:, -1] = 0

    exit()

    #Ans = []
    #for x1, y1, x2, y2 in XYXY:
    for _ in range(Q):
        x1, y1, x2, y2 = map(int, input().split())
        ans = 0
        ans += M[x2-1][y2-1] + M[x1-2][y1-2] - M[x2-1][y1-2] - M[x1-2][y2-1]
        #print(ans)
        ans -= L[x2-1][y1-1] - L[x1-2][y1-1]
        ans -= R[x2-1][y2-1] - R[x1-2][y2-1]
        ans -= U[x1-1][y2-1] - U[x1-1][y1-2]
        ans -= D[x2-1][y2-1] - D[x2-1][y1-2]
        assert ans % 2 == 0, ans
        #Ans.append(ans//2)
        print(str(ans//2))
    #print("\n".join(map(str, Ans)))

main()



def main():
    import sys
    import numpy as np
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x=="1", input())) for _ in range(H)] + [[False]*(W+1)]
    S = np.array(S, dtype=np.int32)
    M = np.zeros((H + 1, W + 1), dtype=np.int32)
    L = np.zeros((H + 1, W + 1), dtype=np.int32)
    R = np.zeros((H + 1, W + 1), dtype=np.int32)
    U = np.zeros((H + 1, W + 1), dtype=np.int32)
    D = np.zeros((H + 1, W + 1), dtype=np.int32)

    ud = S[1:, :] * S[:-1, :]
    M[1:, :] += ud
    U[1:, :] -= ud
    M[:-1, :] += ud
    D[:-1, :] -= ud
    del ud
    lr = S[:, 1:] * S[:, :-1]
    M[:, 1:] += lr
    L[:, 1:] -= lr
    M[:, :-1] += lr
    R[:, :-1] -= lr
    del lr
    M = S * 2 - M

    M = M.cumsum(axis=0).cumsum(axis=1)
    L = L.cumsum(axis=0)
    R = R.cumsum(axis=0)
    U = U.cumsum(axis=1)
    D = D.cumsum(axis=1)
    M[:, -1] = 0
    M[-1, :] = 0
    L[-1, :] = 0
    R[-1, :] = 0
    U[:, -1] = 0
    D[:, -1] = 0

    exit()

    #Ans = []
    #for x1, y1, x2, y2 in XYXY:
    for _ in range(Q):
        x1, y1, x2, y2 = map(int, input().split())
        ans = 0
        ans += M[x2-1][y2-1] + M[x1-2][y1-2] - M[x2-1][y1-2] - M[x1-2][y2-1]
        #print(ans)
        ans -= L[x2-1][y1-1] - L[x1-2][y1-1]
        ans -= R[x2-1][y2-1] - R[x1-2][y2-1]
        ans -= U[x1-1][y2-1] - U[x1-1][y1-2]
        ans -= D[x2-1][y2-1] - D[x2-1][y1-2]
        assert ans % 2 == 0, ans
        #Ans.append(ans//2)
        print(str(ans//2))
    #print("\n".join(map(str, Ans)))

main()


def input_test():
    import sys
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x == "1", input())) for _ in range(H)]
    for _ in range(Q):
        x1, y1, x2, y2 = map(int, input().split())
        
input_test()



def main():
    import sys
    import numpy as np
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x=="1", input())) for _ in range(H)] + [[False]*(W+1)]
    S = np.array(S, dtype=np.int8)
    M = np.zeros((H + 1, W + 1), dtype=np.int32)
    L = np.zeros((H + 1, W + 1), dtype=np.int16)
    R = np.zeros((H + 1, W + 1), dtype=np.int16)
    U = np.zeros((H + 1, W + 1), dtype=np.int16)
    D = np.zeros((H + 1, W + 1), dtype=np.int16)

    ud = S[1:, :] * S[:-1, :]
    M[1:, :] += ud
    U[1:, :] -= ud
    M[:-1, :] += ud
    D[:-1, :] -= ud
    del ud
    lr = S[:, 1:] * S[:, :-1]
    M[:, 1:] += lr
    L[:, 1:] -= lr
    M[:, :-1] += lr
    R[:, :-1] -= lr
    del lr
    M = S * 2 - M

    M = M.cumsum(axis=0).cumsum(axis=1)
    L = L.cumsum(axis=0)
    R = R.cumsum(axis=0)
    U = U.cumsum(axis=1)
    D = D.cumsum(axis=1)
    M[:, -1] = 0
    M[-1, :] = 0
    L[-1, :] = 0
    R[-1, :] = 0
    U[:, -1] = 0
    D[:, -1] = 0

    exit()

    #Ans = []
    #for x1, y1, x2, y2 in XYXY:
    for _ in range(Q):
        x1, y1, x2, y2 = map(int, input().split())
        ans = 0
        ans += M[x2-1][y2-1] + M[x1-2][y1-2] - M[x2-1][y1-2] - M[x1-2][y2-1]
        #print(ans)
        ans -= L[x2-1][y1-1] - L[x1-2][y1-1]
        ans -= R[x2-1][y2-1] - R[x1-2][y2-1]
        ans -= U[x1-1][y2-1] - U[x1-1][y1-2]
        ans -= D[x2-1][y2-1] - D[x2-1][y1-2]
        assert ans % 2 == 0, ans
        #Ans.append(ans//2)
        print(str(ans//2))
    #print("\n".join(map(str, Ans)))

main()



def main():
    import sys
    import numpy as np
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x=="1", input())) for _ in range(H)] + [[False]*(W+1)]
    S = np.array(S, dtype=np.int32)
    M = np.zeros((H + 1, W + 1), dtype=np.int32)
    L = np.zeros((H + 1, W + 1), dtype=np.int32)
    R = np.zeros((H + 1, W + 1), dtype=np.int32)
    U = np.zeros((H + 1, W + 1), dtype=np.int32)
    D = np.zeros((H + 1, W + 1), dtype=np.int32)

    ud = S[1:, :] * S[:-1, :]
    M[1:, :] += ud
    U[1:, :] -= ud
    M[:-1, :] += ud
    D[:-1, :] -= ud
    lr = S[:, 1:] * S[:, :-1]
    M[:, 1:] += lr
    L[:, 1:] -= lr
    M[:, :-1] += lr
    R[:, :-1] -= lr
    M = S * 2 - M

    M = M.cumsum(axis=0).cumsum(axis=1)
    L = L.cumsum(axis=0)
    R = R.cumsum(axis=0)
    U = U.cumsum(axis=1)
    D = D.cumsum(axis=1)
    M[:, -1] = 0
    M[-1, :] = 0
    L[-1, :] = 0
    R[-1, :] = 0
    U[:, -1] = 0
    D[:, -1] = 0

    exit()

    #Ans = []
    #for x1, y1, x2, y2 in XYXY:
    for _ in range(Q):
        x1, y1, x2, y2 = map(int, input().split())
        ans = 0
        ans += M[x2-1][y2-1] + M[x1-2][y1-2] - M[x2-1][y1-2] - M[x1-2][y2-1]
        #print(ans)
        ans -= L[x2-1][y1-1] - L[x1-2][y1-1]
        ans -= R[x2-1][y2-1] - R[x1-2][y2-1]
        ans -= U[x1-1][y2-1] - U[x1-1][y1-2]
        ans -= D[x2-1][y2-1] - D[x2-1][y1-2]
        assert ans % 2 == 0, ans
        #Ans.append(ans//2)
        print(str(ans//2))
    #print("\n".join(map(str, Ans)))

main()


def main():
    import sys
    import numpy as np
    input = sys.stdin.readline
    H, W, Q = map(int, input().split())
    S = [list(map(lambda x: x=="1", input())) for _ in range(H)] + [[False]*(W+1)]
    S = np.array(S, dtype=np.int16)
    M = np.zeros((H + 1, W + 1), dtype=np.int32)

    UD = S[1:, :] * S[:-1, :]
    M[1:, :] += UD
    M[:-1, :] += UD

    LR = S[:, 1:] * S[:, :-1]
    M[:, 1:] += LR
    M[:, :-1] += LR

    M = S * 2 - M
    M = M.cumsum(axis=0).cumsum(axis=1)
    UD = UD.cumsum(axis=1, dtype=np.int16)
    LR = LR.cumsum(axis=0, dtype=np.int16)
    M[:, -1] = 0
    M[-1, :] = 0

    del S

    UD = np.pad(UD, ((0, 1), (0, 1)), mode="constant")
    LR = np.pad(LR, ((0, 1), (0, 1)), mode="constant")
    x1, y1, x2, y2 = np.array(list(map(int, sys.stdin.read().split())), dtype=np.int16).reshape(-1, 4).T
    Ans = M[x2-1, y2-1] + M[x1-2, y1-2] - M[x2-1, y1-2] - M[x1-2, y2-1]
    Ans += LR[x2-1, y1-2] - LR[x1-2, y1-2]
    Ans += LR[x2-1, y2-1] - LR[x1-2, y2-1]
    Ans += UD[x1-2, y2-1] - UD[x1-2, y1-2]
    Ans += UD[x2-1, y2-1] - UD[x2-1, y1-2]
    Ans //= 2
    print("\n".join(str(int(a)) for a in Ans))

main()


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted