给搞py大伙们看点东西

P3397 地毯

ErgouTree @ 2024-03-20 21:36:47

那个差分数组多输出一圈零差分会好搞的许多

n, m = map(int, input().split())
diff = [[0] * (n+2) for _ in range(n+2)]
left_up = []
right_down = []
for _ in range(m):
    line = list(map(int, input().split()))
    left_up.append([line[0], line[1]])
    right_down.append([line[2], line[3]])

a = [[0]*(n+2) for _ in range(n+2)]

def output(a, n):
    for i in range(1, n+1):
        print(" ".join(map(str, a[i][1:n+1])))

# 矩阵(x1, y1)-(x2, y2)需要增加元素 x
def area_add(diff_list, x1, y1, x2, y2, x):
    diff_list[x1][y1] += x
    diff_list[x2+1][y1] -= x
    diff_list[x1][y2+1] -= x
    diff_list[x2+1][y2+1] += x

for i in range(len(left_up)):
    x1, y1 = left_up[i][0], left_up[i][1]
    x2, y2 = right_down[i][0], right_down[i][1]
    area_add(diff, x1, y1, x2, y2, 1)

for i in range(1, n+1):
    for j in range(1, n+1):
        a[i][j] = a[i][j-1] + a[i-1][j] - a[i-1][j-1] + diff[i][j]

output(a, n)

|