求助!!!python 暴力能得20分 修改成差分后全是WA,但是样例好像能过

P3397 地毯

ylly333 @ 2022-05-15 16:45:51

这是暴力代码

# 暴力
n, m = map(int, input().split())
lis = [[0 for i in range(n)] for j in range(n)]
for t in range(m):
    x1, y1, x2, y2 = map(int, input().split())
    for x in range(x1-1, x2):
        for y in range(y1-1, y2):
            lis[x][y] += 1

for li in lis:
    print(*li)

这是改成差分得代码

n, m = map(int, input().split())
lis = [[0 for i in range(n)] for j in range(n)]
flag = [[0 for h in range(n+1)] for k in range(n)]
for t in range(m):
    x1, y1, x2, y2 = map(int, input().split())
    for x in range(x1, x2 + 1):
        flag[x-1][y1 - 1] = 1
        flag[x-1][y2] = -1

# for f in flag:
#     print(*f)
for x in range(len(lis)):
    s = 0
    for y in range(len(lis[x])):
        s += flag[x][y]
        lis[x][y] = s
# 输出
for l in lis:
    print(*l)

|